Skip to content Skip to sidebar Skip to footer

Tkinter - Run Function After Frame Is Displayed

The code below has an onShowFrame function in the OtherPage class that runs when the OtherPage frame is shown. My problem is that everything in the onShowFrame function runs befor

Solution 1:

The problem is related to the fact that you're calling sleepwhich causes the application to sleep. In order for the display to update, the event loop has to be able to process events, which it cannot do while sleeping. To sleep literally means to stop all processing. The first six words of the documentation for time.sleep is Suspend execution of the current thread.

This is one place where calling update on a widget is a reasonable thing to do. It will give tkinter a chance to process all pending events -- such as the finishing of handling a button click, redrawing the window, etc -- before generating the new event.

It's as easy as changing your function to look like this:

defshowFrame(self,cont):
    frame = self.frames[cont]
    frame.tkraise()
    frame.update()
    frame.event_generate("<<ShowFrame>>")

Post a Comment for "Tkinter - Run Function After Frame Is Displayed"