我在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