python - tkinter root window isn't quite the right size -


i creating tkinter canvas, , need check when user changes size of window. problem is, window apparently isn't size it's supposed be. have following:

def print_size(self):     print self.root.winfo_width()  def init_simulation(self, size=300):     self.root = tk()     canvas = canvas(self.root, width=size, height=size)     self.print_size()     self.root.after(1000, self.print_size) 

when running get:

1 

and second later:

306 

ignoring fact tkinter add 6 pixels, why size first 1 , 306? setting wrong?

when instantiate root widget tk(), tkinter starts process in separate thread create window - doesn't happen in main loop.

the reason 1 size root window doesn't exist yet when call self.print_size first time, gives default value of 1. next time call second later, window has finished spawning, gives the actual size. it's race condition - main event loop gets print self.root.winfo_width() before self.root done being created.

if you'd change behavior, add line right after self.root = tk():

self.root.wait_visibility(self.root) 

that command forces main event loop pause until given widget (in case root window) has been spawned , visible.

also, note you've set size of canvas 300 pixels, naturally container window have width.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo