我在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函数分配给&#34;打印分数&#34;按钮显示正确的分数......似乎它不能从最初显示实际值,但我们必须单击按钮才能这样做...
#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()
答案 0 :(得分:1)
您的代码存在一些主要问题。我认为在使用类时使用全局变量会产生反作用。
您的代码无法正常工作的一个主要原因是您使用的是应该使用类属性的变量。如果在类中使用变量,那么在__init__
之后,您将无法与该变量进行交互。无论是小部件还是保存值,如数字或字符串。
我们可以通过将self.
前缀添加到要从类内部或类对象外部进行交互的任何内容来解决此问题。
此外,您的代码实际上并未显示您有一个名为score的全局变量,因此我在全局命名空间中添加了一个用于测试。
考虑到这一点,您正在为多个标签使用变量名称标签。无论何时分配变量名称,它们都必须是唯一的。
我已经将代码的3个部分组合在一起,以提供一个可以更新分数的工作示例。这个例子并不完美,但它是为了得到你想要的结果而需要改变的最小值。
如果您有任何疑问,请与我们联系:
import tkinter as tk
from tkinter import font as tkfont
score = 1
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)
self.controll15 = tk.IntVar()
wrong151 = tk.Radiobutton(self, text="Neural Networks work bad with small amount of data",
variable= self.controll15,value=1)
wrong152 = tk.Radiobutton(self, text="Concept of neural network exists since the middle of the mid-twentieth",
variable= self.controll15,value=0)
right15 = tk.Radiobutton(self, text="There is no learning rate parameter in training neural networks",
variable= self.controll15,value=5)
wrong151.place(x=0, y=60)
wrong152.place(x=0, y=80)
right15.place(x=0, y=100)
button = tk.Button(self, text="Result",command = lambda: controller.show_frame("Results"))
button.bind("<Button-1>", self.scorer)
button.pack(side="right")
def scorer(self, event = None):
if int(self.controll15.get()) > 2:
global score
score += 1
self.controller.frames["Results"].update_label()
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")
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 (theme1Q5, Results):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("theme1Q5")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
#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("theme1Q5"))
button.pack(side= "bottom")
global score
self.label2 = tk.Label(self, text= "%s / 5" %(score), font=self.controller.title_font)
self.label2.pack()
def update_label(self):
global score
self.label2.config(text= "%s / 5" %(score))
print(score)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()