2016-01-26 50 views
0

没有经过任何正式培训和对Python 3.3的基本理解,我正在努力了解tkinter是否能够成为基本的gui。我已阅读了这种方法:清除tkinter中的帧

Tkinter! Understanding how to switch frames

这是大大超过我的头。我没有从复制和粘贴代码中学得很好。我的问题是这样的:

如果我想用多个窗口来创建一个GUI,我通过简单地销毁一个框架并使用一个类替换另一个窗口,我想我只是不明白为什么它是如此的困难难。今天我玩了一段时间,我不明白为什么下面的代码不起作用。我不能包裹面条的是为什么它正确地翻转到新窗口,但未能返回到主窗口。两个按钮的编码方式都是相同的,但其中一个可以工作,一个不可以。

from tkinter import * 

class Application: 
    def __init__(self, master): 
     self.master = master 
     self.new_switch_on = False 
     self.main_switch_on = False 
     self.main_frame() 

    def main_frame(self): 
     if self.new_switch_on == True: 
      self.new_frame.destroy() 
     self.new_switch_on = False 
     self.main_switch_on = True 
     self.main_frame = Frame(self.master, width = 200, height = 100) 
     Label(self.main_frame, text = 'Main Frame').pack() 
     Button(self.main_frame, text = 'Next', command = self.new_frame).pack() 
     self.main_frame.pack() 

    def new_frame(self): 
     if self.main_switch_on == True: 
      self.main_frame.destroy() 
     self.main_switch_on = False 
     self.new_switch_on = True 
     self.new_frame = Frame(self.master, width = 400, height = 200) 
     Label(self.new_frame, text = 'New Frame').pack() 
     Button(self.new_frame, text = 'Back', command = self.main_frame).pack() 
     self.new_frame.pack() 

root = Tk() 
root.geometry('-1+1') 

app = Application(root) 

root.mainloop() 

我知道我可以按照上述链接以获得期望的结果,但我只是不明白为什么这样的事情,否则的东西是一个新的学习不能完成更简单一点相同的结果。

回答

1

问题是因为您使用方法main_frame(self, ...)和框架self.main_frame = Frame(...)相同的名称。

当您将帧分配给变量self.main_frame = Frame(...)时,则无法访问方法self.main_frame(),并且您的Button(self.new_frame, text = 'Back', command=self.main_frame)无法调用方法self.main_frame

使用方法main_frame不同名称 - 例如create_main_frame

同样的问题是与new_frame(self, ...)self.new_frame = Frame(...)

(PS我添加到代码空行,使其更具有可读性。)

from tkinter import * 

class Application: 

    def __init__(self, master): 
     self.master = master 

     self.new_switch_on = False 
     self.main_switch_on = False 

     self.create_main_frame() 

    def create_main_frame(self): # new name 
     if self.new_switch_on == True: 
      self.new_frame.destroy() 

     self.new_switch_on = False 
     self.main_switch_on = True 

     self.main_frame = Frame(self.master, width = 200, height = 100) 

     Label(self.main_frame, text = 'Main Frame').pack() 
     Button(self.main_frame, text = 'Next', command = self.create_new_frame).pack() # new name 

     self.main_frame.pack() 

    def create_new_frame(self): # new name 
     if self.main_switch_on == True: 
      self.main_frame.destroy() 

     self.main_switch_on = False 
     self.new_switch_on = True 

     self.new_frame = Frame(self.master, width = 400, height = 200) 

     Label(self.new_frame, text='New Frame').pack() 
     Button(self.new_frame, text='Back', command=self.create_main_frame).pack() # new name 

     self.new_frame.pack() 


root = Tk() 
root.geometry('-1+1') 

app = Application(root) 

root.mainloop() 
+0

是的......我觉得自己像一个白痴,因为我知道比做这个更好。感谢您指出它。完全按照预期工作,现在消除了布尔和改变一些东西后,它仍然完美地工作。 – Gregory6106