2017-08-25 197 views
0

我在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() 
+0

都是一樣的Python文件的3頁的一部分? – user3535074

+0

是的,它們都是一個文件的一部分。 – Jakun

+0

你在哪裏_define_「得分」? – Lafexlos

回答

1

您的代碼存在一些主要問題。我認爲在使用類時使用全局變量是反效果的。

您的代碼無法正常工作的主要原因之一是由於您正在使用應該使用類屬性的變量。如果在類中使用變量,則在__init__之後,將無法與該變量進行交互。無論是小部件還是像數字或字符串一樣的保存值。

我們可以通過將self.前綴添加到要從類內部或類對象外部進行交互的任何內容來解決此問題。

此外,您的代碼實際上並未顯示您有一個稱爲分數的全局變量,因此我在全局命名空間中添加了一個用於測試的代碼。

考慮到這一點,你使用多個標籤的變量名稱標籤。任何時候你分配一個變量名稱,他們需要是唯一的。

我組合了代碼的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()