การใช้งาน GUI Tkinter เป็นโมดูลของภาษา Python ที่นำมาสร้างหน้าต่างโปรแรมได้มากมาย หลากหลายรูปแบบตามที่ผู้งานติองการสร้างขึ้นมา การเรียกฟังก์ชันต่าง ๆ มาใช้งานก็มีมายมายให้เลือกใช้งาน จะมายกตัวอย่างการสร้างโปรแกรมคำถามขึ้นมา
เป็นการสร้างหน้าต่าง Quiz ขึ้นมา มีคำถามให้ทำจำนวน 5 ข้อ เมื่อตอบถูกจะมีหน้าต่างขึ้นว่า Correct แต่ถ้าหากตอบผิดจะขึ้นหน้าต่างว่า Again!! สามารถเลือกข้อที่จะทำได้โดยกด next เพื่อไปข้างหน้า กด prev เพื่อย้อนกับ
การสร้างโปรแกรมนี้ถือเป็นตัวอย่างการทำคำถามขึ้นมา ในความจริงสามารถทำได้หลายรูปแบบ ตามความต้องการของผู้ใช้งาน สามารถนำตัวอย่างนี้เป็นแบบในการสร้างได้
ตัวอย่าง
โค้ด: เลือกทั้งหมด
from Tkinter import *
from tkMessageBox import *
class Problem(object):
def __init__(self, question="", a="", b="", c="", correct=""):
object.__init__(self)
self.question = question
self.a = a
self.b = b
self.c = c
self.correct = correct
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.problems = []
self.counter = 0
self.addComponents()
self.loadProblems()
self.showProblem(0)
self.mainloop()
def addComponents(self):
self.title("Quiz")
self.grid()
self.columnconfigure(0, minsize=100)
self.columnconfigure(1, minsize=200)
self.columnconfigure(2, minsize=100)
self.lblQuestion = Label(self, text="Question")
self.lblQuestion.grid(columnspan=3, sticky="we")
self.btnA = Button(self, text="A", command=self.checkA)
self.btnA.grid(columnspan=3, sticky="we")
self.btnB = Button(self, text="B", command=self.checkB)
self.btnB.grid(columnspan=3, sticky="we")
self.btnC = Button(self, text="C", command=self.checkC)
self.btnC.grid(columnspan=3, sticky="we")
self.btnPrev = Button(self, text="prev", command=self.prev)
self.btnPrev.grid(row=4, column=0)
self.lblCounter = Label(self, text="0")
self.lblCounter.grid(row=4, column=1)
self.btnNext = Button(self, text="next", command=self.next)
self.btnNext.grid(row=4, column=2)
def checkA(self):
self.check("A")
def checkB(self):
self.check("B")
def checkC(self):
self.check("C")
def check(self, guess):
correct = self.problems[self.counter].correct
if guess == correct:
showinfo("Quiz", "Correct")
else:
showinfo("Quiz", "Again!!")
def prev(self):
self.counter -= 1
if self.counter < 0:
self.counter = 0
self.showProblem(self.counter)
def next(self):
self.counter += 1
if self.counter >= len(self.problems):
self.counter = len(self.problems) - 1
self.showProblem(self.counter)
def showProblem(self, counter):
self.lblQuestion["text"] = self.problems[counter].question
self.btnA["text"] = self.problems[counter].a
self.btnB["text"] = self.problems[counter].b
self.btnC["text"] = self.problems[counter].c
self.lblCounter["text"] = self.counter
def loadProblems(self):
self.problems.append(Problem(
"Where are you ?",
"Bangkok",
"Nakhon Sawan",
"Phitsanulok",
"A"))
self.problems.append(Problem(
"What is your quest?",
"Work",
"Sleep",
"Eat",
"B"))
self.problems.append(Problem(
"We are the knights who say...",
"nothing",
"I am God",
"Kill it",
"C"))
self.problems.append(Problem(
"What is your favorite color?",
"Red",
"Yellow",
"Green",
"C"))
self.problems.append(Problem(
"The movie you like.?",
"Captain america",
"Iron man",
"Thor",
"A"))
def main():
a = App()
if __name__ == "__main__":
main()
เมื่อตอบถูก
เมื่อตอบผิด
บทความที่เกี่ยวข้อง
บทเรียน Python
VDO Tutorial - Python
บทเรียน Python Tensorflow
บทเรียน Python GUI