If I have a function called do_stuff()
, how do I execute it AFTER a tkinter window named root
finishes loading.
Asked
Active
Viewed 2,656 times
3

Person
- 379
- 1
- 3
- 15
-
You can use `after` method with some estimated time. – Piotr Wasilewicz Mar 23 '18 at 06:08
-
Explain, please – Person Mar 23 '18 at 06:25
-
https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method You can execute any method after some time. – Piotr Wasilewicz Mar 23 '18 at 06:26
-
1While `after` is a viable option, it's worth to notice, that [`Map/Unmap`](http://tcl.tk/man/tcl8.5/TkCmd/bind.htm#M13) event is another option. – CommonSense Mar 23 '18 at 07:37
-
Have you done any research before asking this question? See [How much research effort is expected of Stack Overflow users?](http://meta.stackoverflow.com/q/261592/7432) – Bryan Oakley Mar 23 '18 at 11:55
2 Answers
1
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

Christian Reall-Fluharty
- 797
- 7
- 20
-1
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.

patthoyts
- 32,320
- 3
- 62
- 93
-
1
-
The question asked about fully loaded window aka all inner widgets are loaded as well.
is triggered earlier. You can check it by updating the window geometry and then binding window – Andrey Dec 08 '22 at 16:45to print(window.geometry()). The reported value will differ from the required one.