While the docs give some insight on how winfo_height/width
, I've come across an unexpected result when positioning a window and using overrideredirect
.
The following creates a root window with no title bar and border:
>>> import tkinter as tk
>>> root = tk.Tk()
>>> sw = root.winfo_screenwidth() # 1080
>>> sh = root.winfo_screenheight() # 1920
>>> xoff = (sw // 2) - 200
>>> yoff = (sh // 2) - 100
>>> root.geometry(f'400x200+{xoff}+{yoff}')
>>> root.overrideredirect(True)
This window is centered in the screen and has a size of 400x200. Calling some methods on it:
>>> root.winfo_geometry()
'400x200+760+440'
>>> root.winfo_rooty()
440
>>> root.winfo_y()
440
>>> root.winfo_rootx()
760
>>> root.winfo_x()
760
>>> root.winfo_width()
400
>>> root.winfo_height()
200
This behaves as expected; the upper left corner of the root window is where it should be and all geometries align as intended. But supposing the title bar and border are re-enabled:
>>> root.overrideredirect(False)
>>> root.winfo_geometry()
'400x200+760+440'
>>> root.winfo_rooty()
471
>>> root.winfo_y()
440
>>> root.winfo_rootx()
768
>>> root.winfo_x()
760
>>> root.winfo_width()
400
>>> root.winfo_height()
200
A border now appears around the window along with a title bar, the upper left corner remains where it was but all other corners have moved (upper right to the the right, lower left down and lower right down and to the right). Yet calls to winfo_height/width
remain unchanged. Why are they not updated to include the true width of the entire window and its bordering content?
Of note, the value returned by winfo_height/width
may be the true value given that the geometry is set before overrideredirect(True)
is called; if that's the case, then why aren't the values returned smaller when there is no border?
Edit:
A similar question regarding geometries and the bordering graphics