我在tkinter中创建了一个测验,该测验从一个页面到另一个页面,并添加到变量“score”中。 但是,如果我想显示最终得分,它显示的是初始值而不是实际值。 有人有类似的问题或解决方案的想法?标签Tkinter变量
感谢
import tkinter as tk
from tkinter import font as tkfont
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,theme1page,theme2page,theme1Q1,theme1Q2,theme1Q3,\
theme1Q4,theme1Q5,theme2Q1,theme2Q2,theme2Q3,theme2Q4,theme2Q5, Results):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
我们直接显示为简便起见,最后一个问题页面。可变分数是我们在正确回答时增加的分数。
class theme1Q5(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="5.Question", font=controller.title_font)
label.pack(side="top", fill="x")
Question15 = tk.Label(self, text="Which sentence is not true?")
Question15.place(x=0, y = 30)
controll15 = tk.IntVar()
wrong151 = tk.Radiobutton(self, text="Neural Networks work bad with small amount of data", \
variable= controll15,value=1)
wrong152 = tk.Radiobutton(self, text="Concept of neural network exists since the middle of the mid-twentieth",\
variable= controll15,value=0)
right15 = tk.Radiobutton(self, text="There is no learning rate parameter in training neural networks", \
variable= controll15,value=5)
wrong151.place(x=0, y=60)
wrong152.place(x=0, y=80)
right15.place(x=0, y=100)
def scorer(event):
if int(controll15.get()) > 2:
global score
score += 1
button = tk.Button(self, text="Result",command = lambda: controller.show_frame("Results"))
button.bind("<Button-1>", scorer)
button.pack(side="right")
#END THEME 1
这是显示实际结果(分数值)的页面。问题是,在正确回答所有问题的同时,它会显示得分初始值(0)。另一方面,分配给“打印分数”按钮的scorecalc函数显示正确的分数...似乎它不能从第一个实际值显示,但我们必须点击按钮才能这样做...
#RESULT PAGE
class Results(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Your Score:", font=controller.title_font)
label.pack(side="top", fill="x")
button = tk.Button(self, text="return to Menu",
command=lambda: controller.show_frame("StartPage"))
button.pack(side= "bottom")
global score
label = tk.Label(self, text= "%s/5" %(score), font=controller.title_font)
label.pack()
def scorecalc():
label = tk.Label(self, text="Your Score:", font=controller.title_font)
label.pack(side="top", fill="x")
label = tk.Label(self, text= "%s/5" %(score), font=controller.title_font)
label.pack()
scorep= tk.Button(self, text ="print score", command=scorecalc)
scorep.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
都是一样的Python文件的3页的一部分? – user3535074
是的,它们都是一个文件的一部分。 – Jakun
你在哪里_define_“得分”? – Lafexlos