2017-08-25 196 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()