Tkinter 6 – Labels in tkinter
A little example about making a little app with tkinter to make a multiple choice interactive quiz with just labels and the bind method. Just some lines of code with a couple of classes and a function to make a nice test. You can start from this code to make a more complex test that your students could make or to learn something about tkinter.
The video: live coding to make a quiz with python and tkinter
The following video was made “live” and it lasts 14 minutes. You will see how it is possible to make a quiz with tkinter and just labels. Not a very complicated stuff, but it works and it shows something like classes to make labels in a fastest way for the purpose of the app.
import tkinter as tk
root = tk.Tk()
def check(answer):
if answer[-1] == "*":
print("Right, it is {}".format(answer[:-1]))
else:
print("Wrong")
class Question:
def __init__(self, question):
self.question = question
self.label = tk.Label(root, text=question)
self.label.pack()
class Answer:
def __init__(self, answer):
self.answer = answer
self.label = tk.Label(root, text=answer[:-1])
self.label.bind("<Button-1>", lambda x: check(answer))
self.label.pack()
# Put here the question and answers separated by comma
# right answer ends with *, wrong one ends with -
q = """What is the capital of Italy?, Rome*, Paris-, London-,
What is the capital of France?, Rome-, Paris*, London-
"""
q = q.splitlines()
q2 = []
for line in q:
q2.append(line.split(","))
q = q2
for quest in q:
Question(quest[0])
for ans in quest[1:]:
Answer(ans)
root.mainloop()
The output
Updated 7.12.2019 – v. 1.1
Try this test too
This is another way to make a test wit Python
import tkinter as tk
from random import shuffle
f = """Denaro in Cassa
Banca x c/c
Crediti v/clienti
Debiti v/fornitori
Iva a ns credito
Iva a ns debito
Mutui passivi
Ratei passivi
Ratei crediti
""".splitlines()
e = """Merci c/acquisti
Merci c/vendite
Attrezzature
Stipendi
Arredi
Brevetti
Software
Computer
Tovagliato
Utenze elettriche
Utenze telefoniche
""".splitlines()
num_questions = len(f) + len(e)
fe = f + e
shuffle(fe)
answer = []
for q in fe:
if q in f:
answer.append("f")
else:
answer.append("e")
quest = []
for n in range(num_questions):
quest.append((fe[n], answer[n]))
print(quest)
numdom = len(quest)
score = 0
num = 0
def d1():
global num, score, entry
if num == numdom:
text.pack_forget()
entry.pack_forget()
score = score / num * 100
button['text'] = f"Score {score}%\n Click to Close this window"
button['command'] = game_over
button.pack()
return
if num == 0:
answer_widget()
text['height'] = 7
text['bg'] = 'cyan'
text['width'] = 80
text.delete("1.0",tk.END)
text.insert("1.0",quest[num][0] + "\n\n Choose among [f][e]")
button.pack_forget()
num+=1
def game_over():
global score
print(score)
root.destroy()
def answer_widget():
global entry
entry = tk.Entry(root, textvariable=solution, bg="yellow", font="Arial 20")
entry.pack()
entry.bind("<Return>", lambda x: check())
entry.focus()
def empty_textbox():
solution.set("")
d1()
def check():
global n, score
text.delete("1.0",tk.END)
if solution.get() == quest[num-1][1]:
text.insert(tk.END, "Right +1")
score+=1
text['bg'] = "green"
else:
text.insert(tk.END, "Wrong 0 points")
text['bg'] = "red"
label['text'] = score
text.after(1000, empty_textbox)
root = tk.Tk()
label = tk.Label(root, text = """Test""", bg="coral", font="Arial 48")
label.pack()
rules = """Answer to the Following questions
Click on the button below to start
You will see a question
Write your answer and press Enter
"""
text = tk.Text(root, height=12,font="Arial 20")
text.insert("1.0", rules)
text.pack()
button = tk.Button(root, text="Click To Start", bg="black", fg="white", command=d1, font="Arial 20")
button.pack()
solution = tk.StringVar()
root.mainloop()
Output
Tkinter test for students
Tkinter articles


