YusufChowdhury
YusufChowdhury

Reputation: 349

Running Super when frame is shown - Python Tkinter

I have this frame that is displayed however, I want the last line to run only when this frame is actually shown (and not when the program runs). Things are happening in the function start which I only want happening when this frame is shown

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()

EDIT Here is my show_frame method:

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>>")

Upvotes: 0

Views: 149

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

The main problem is that you are using code you don't understand. You should probably start with a simpler architecture until you are able to understand how it works.

That being said, there are several simple solutions. For example, you can modify the show_frame method to either directly call a method in each page, or it can send an event which you can bind a function to.

For example:

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()

Note: since FrameTwo inherits from reviseTen, you shouldn't call Super(FrameTwo, self).start(), you can just call self.start().

Upvotes: 3

Related Questions