2

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

Person
  • 329
  • 1
  • 2
  • 14

2 Answers2

0

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
  • 30,898
  • 3
  • 60
  • 89
0

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
  • While `` probably is better for some purposes, I was trying to center the window, and the geometry didn't seem finalized by the time `` was triggered, so `` was the next best option – Christian Reall-Fluharty Oct 23 '20 at 05:56