How to run a function after a tkinter window is loaded

3k views Asked by At

If I have a called do_stuff(), how do I execute it AFTER a window named root finishes loading.

2

There are 2 answers

2
patthoyts On

When a window is placed on screen in X Windows is has been mapped so the Tk <Map> event is raised to let your application know that this window is now created and on-screen. If you only want to handle this once after creation then delete your binding the first time your receive the event as it is sent each time the window is re-mapped on screen. ie: minimize and restore events.

1
creallf On

Similar to the <Map> event, the <Visibility> event is triggered whenever the window/widget becomes visible. By unbinding in the callback, we can make sure the callback is only called once when the window becomes visible.

def callback():
    # your code here
    root.unbind('<Visibility>') # only call `callback` the first time `root` becomes visible

root.bind('<Visibility>', callback) # call `callback` whenever `root` becomes visible