0

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

  • Perhaps here in the doc there is something: "Setting the override-redirect flag for a window causes it to be ignored by the window manager; among other things, this means that the window will not be reparented from the root window into a decorative frame and the user will not be able to manipulate the window using the normal window manager mechanisms. Note that the override-redirect flag is only guaranteed to be taken notice of when the window is first mapped or when mapped after the state is changed from withdrawn to normal. Some, but not all, platforms will take notice at additional times." – hussic Feb 21 '21 at 11:22
  • @hussic unfortunately that does not answer why the height and width remain the same when a border is present/removed; if they are returning the geometries for when a border is present, then the values returned after `overrideredirect` is called should be smaller (or vice versa). –  Feb 21 '21 at 20:04
  • From my tests on w7 the geometry widht/height is always the size of the inner box without decorations. overrideredirect cannot change it. – hussic Feb 22 '21 at 12:52

1 Answers1

0

The total height of a root window can be printed with (w7):

print('total height:', root.winfo_rooty() + root.winfo_height() - root.winfo_y())

the same can be done for width.

To be more exact: the bottom border is not added to the heigth.

hussic
  • 1,816
  • 9
  • 10