Python Tkinter how to adjust the x,y default 0,0 coordinates Python Tkinter how to adjust the x,y default 0,0 coordinates tkinter tkinter

Python Tkinter how to adjust the x,y default 0,0 coordinates


Ok, there are two thing that you are missing. The first, in your first example, is that you need to assure tkinter is getting the right values, and to do this you have to use update_idletasks() method before any winfo.

The second thing, which explains why you have to use -8 to center the full screen window, is that tkinter widows have outer-frames. You can determine the size of this frame by checking for the top left coordinate of the window (winfo_rootx()) and of the outer-frame (winfo_x()). Now the frame width is the difference between both: frame_width = root.winfo_rootx() - root.winfo_x() while the real window width is real_width = root.winfo_width() + (2*frame_width), since you have to consider the frame in both sides.

In summary, to center the window horizontally you do:

width = root.winfo_width()frame_width = root.winfo_rootx() - root.winfo_x()real_width = root.winfo_width() + (2*frame_width)x = root.winfo_screenwidth() // 2 - real_width // 2

(here you can print the frame width and you will see it is 8)

and then use geometry method to place the window at position x.

Off course you can do the same for the vertical alignment