When you instantiate a root widget with Tk()
, Tkinter starts a process in a separate thread to actually create the window - it doesn't happen in the main loop.
The reason you get a 1
for the size initially is that the root window doesn't exist yet when you call self.print_size
the first time, so it gives you a default value of 1
. The next time you call it a second later, the window has finished spawning, so it gives the the actual size. It's essentially a race condition - the main event loop gets to the print self.root.winfo_width()
before self.root is done being created.
If you'd like to change this behavior, add this line right after self.root = Tk()
:
self.root.wait_visibility(self.root)
That command forces the main event loop to pause until the given widget (in this case the root window) has been spawned and is visible.
Also, note that you've set the size of the canvas to 300 pixels, so naturally the container window will have some extra width.