Отображение результата при завершении викторины
Во время написания кода на Python версии 3.10 в Tkinter, встретился с такой проблемой: при запуске кода нажимая на кнопку "старт" и выбирая правильный вариант ответа и нажимая на кнопку "next", должно отобразиться число баллов, но оно не отображается.
Если есть решения на этот вопрос, буду благодарен.
Сам код был взят с сайта: https://learn4kid-python.firebaseapp.com/tkinter_2/tkinter_quiz/#step-1-%D1%88%D0%B0%D0%B3-1-%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82%D1%81%D1%82%D0%B2%D0%B8%D0%B5
Код викторины:
from tkinter import *
from tkinter.ttk import *
answers = []
points = 0
true_answers = "10110"
def check(el):
global points
for t, a in zip(true_answers, answers):
if bool(int(t)) and bool(int(t)) == a.get():
points += 1
elif bool(int(t)) != a.get():
points -= 1
for widget in el.winfo_children():
widget.destroy()
Label(el, text="Your result: ").pack()
Button(el, text="Try again", command=lambda e=el: first_screen(e)).pack()
points = 0
tk = Tk()
tk.title("My Quiz")
tk.geometry("500x500")
frm = LabelFrame(text="Generate Screen")
frm.pack(fill="both", expand="yes")
def ask_question(el):
for widget in el.winfo_children():
widget.destroy()
question = Label(el, text="Which widgets using to display text with the ability to edit it?").pack()
answers.append(BooleanVar())
ch_1 = Checkbutton(el, text="Canvas", variable=answers[0], onvalue=1, offvalue=0).pack(anchor=W)
ch_2 = Checkbutton(el, text="ListBox", variable=answers[0], onvalue=2, offvalue=0).pack(anchor=W)
ch_3 = Checkbutton(el, text="Entry", variable=answers[0], onvalue=3, offvalue=0).pack(anchor=W)
ch_4 = Checkbutton(el, text="Text", variable=answers[0], onvalue=4, offvalue=0).pack(anchor=W)
ch_5 = Checkbutton(el, text="Label", variable=answers[0], onvalue=5, offvalue=0).pack(anchor=W)
button_next = Button(el, text="Next >>", command=lambda e=el: check(e)).pack()
def first_screen(el):
Label(el, text='Welcome to my quiz!\n Press "Start" to continue...').pack()
Button(el, text="Start", command=lambda e=el: ask_question(e)).pack()
first_screen(frm)
tk.mainloop()
Ответы (1 шт):
Автор решения: arnold
→ Ссылка
Всё дело в том, что вы не вывели переменную:
from tkinter import *
from tkinter.ttk import *
answers = []
points = 0
true_answers = "10110"
def check(el):
global points
for t, a in zip(true_answers, answers):
if bool(int(t)) and bool(int(t)) == a.get():
points += 1
elif bool(int(t)) != a.get():
points -= 1
for widget in el.winfo_children():
widget.destroy()
###################################################
Label(el, text="Your result: " + str(points)).pack()
###################################################
Button(el, text="Try again", command=lambda e=el: first_screen(e)).pack()
points = 0
tk = Tk()
tk.title("My Quiz")
tk.geometry("500x500")
frm = LabelFrame(text="Generate Screen")
frm.pack(fill="both", expand="yes")
def ask_question(el):
for widget in el.winfo_children():
widget.destroy()
question = Label(el, text="Which widgets using to display text with the ability to edit it?").pack()
answers.append(BooleanVar())
ch_1 = Checkbutton(el, text="Canvas", variable=answers[0], onvalue=1, offvalue=0).pack(anchor=W)
ch_2 = Checkbutton(el, text="ListBox", variable=answers[0], onvalue=2, offvalue=0).pack(anchor=W)
ch_3 = Checkbutton(el, text="Entry", variable=answers[0], onvalue=3, offvalue=0).pack(anchor=W)
ch_4 = Checkbutton(el, text="Text", variable=answers[0], onvalue=4, offvalue=0).pack(anchor=W)
ch_5 = Checkbutton(el, text="Label", variable=answers[0], onvalue=5, offvalue=0).pack(anchor=W)
button_next = Button(el, text="Next >>", command=lambda e=el: check(e)).pack()
def first_screen(el):
Label(el, text='Welcome to my quiz!\nPress "Start" to continue...').pack()
Button(el, text="Start", command=lambda e=el: ask_question(e)).pack()
first_screen(frm)
tk.mainloop()