2017-03-29 22 views
-1

但是我显示了这个框架,但是只有当这个框架实际显示(而不是程序运行时)时,我才需要最后一行运行。事情在功能启动时,该框架显示当显示框架时运行超级 - Python Tkinter

class FrameTwo(reviseTen): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     self.instructions = tk.Label(self, text="Click on the option you think is correct, then click 'Proceed to next question'") 
     self.startButton = tk.Button(self, text="Click here to start revision session") 
     self.optionOne = tk.Button(self, text="Option One", command=super(FrameTwo, self).clickOptionOne) 
     self.optionTwo = tk.Button(self, text="Option Two", command=super(FrameTwo, self).clickOptionTwo) 
     self.optionThree = tk.Button(self, text="Option Three", command=super(FrameTwo, self).clickOptionThree) 
     self.optionFour = tk.Button(self, text="Option Four", command=super(FrameTwo, self).clickOptionFour) 
     self.question = tk.Label(self, text="What is the definition of: ") 
     self.proceedButton = tk.Button(self, text="Proceed to next question", command=lambda: controller.show_frame(FrameThree)) 
############# EDIT #################### 
     self.bind("<<Show>>", self.do_something) 

def do_something(self, event): 
    self.start() 

编辑在这里,我只希望发生的情况是我show_frame方法:

def show_frame(self, cont): # Method to show the current frame being used 
    for frame in self.frames.values(): 
     frame.grid_remove() 
    frame = self.frames[cont] 
    frame.tkraise() 
    frame.update() 
    frame.grid() 
    frame.event_generate("<<Show>>") 
+0

'class FrameTwo(reviseTen)' - 什么是'reviseTen',为什么'FrameTwo'从它继承?你为什么要调用'tk.Frame .__ init__'而不是'reviseTen .__ init__'或'super().__ init__'? – user2357112

+2

请不要破坏你的帖子。一旦你发布了一个问题,它就一般属于Stack Overflow社区(根据[CC-by-SA许可证](https://creativecommons.org/licenses/by-sa/3.0/))。如果您想要从您的帐户中取消关联此信息,请参阅[解除请求的正确途径是什么?](https://meta.stackoverflow.com/q/323395/5244995)。 –

回答

2

的主要问题是,你正在使用的代码你不明白。你应该从一个更简单的架构开始,直到你能够理解它是如何工作的。

这就是说,有几个简单的解决方案。例如,您可以修改show_frame方法,以便直接在每个页面中调用方法,或者可以发送可以将函数绑定到的事件。

例如:

class SampleApp(tk.Tk): 
    ... 
    def show_frame(self, page_name): 
     '''Show a frame for the given page name''' 
     frame = self.frames[page_name] 
     frame.tkraise() 
     frame.event_generate("<<Show>>") 

class FrameTwo(reviseTen): 
    def __init__(self, parent, controller): 
     ... 
     self.bind("<<Show>>", self.do_something) 

    def do_something(self, event): 
     self.start() 

注意:由于来自reviseTenFrameTwo继承,你不应该叫Super(FrameTwo, self).start(),你可以叫self.start()

+0

我对“自我”错误表示歉意。我更新了答案。 –

+0

@yusufchowdhury:听起来你不正确地做了绑定,但我看不到你的代码,所以我不能肯定地说。确保绑定到'self.do_something'而不是'self.do_something()' –

+0

我绑定的代码和我刚才看到的完全一样,但是,我的show_frame有点不同,我不是能够告诉如果这是造成的问题 - 更新 - 我用这个解决方案的代码:http://stackoverflow.com/questions/36293437/tkinter-run-function-after-frame-is-displayed但它仍然无法正常工作 – YusufChowdhury