new Window.current_location parameter more_accurate.

This commit is contained in:
PySimpleGUI 2021-08-18 10:08:17 -04:00
parent 2b49a36a50
commit bbd3ab6341
1 changed files with 25 additions and 9 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
version = __version__ = "4.46.0.5 Unreleased" version = __version__ = "4.46.0.6 Unreleased"
""" """
Changelog since 4.46.0 release to PyPI on 10 Aug 2021 Changelog since 4.46.0 release to PyPI on 10 Aug 2021
@ -16,6 +16,8 @@ version = __version__ = "4.46.0.5 Unreleased"
4.46.0.5 4.46.0.5
Fix for default element size - was incorrectly using as the default for parm in Window. Fix for default element size - was incorrectly using as the default for parm in Window.
Needed to set it in the init code rather than using the parm to set it. Needed to set it in the init code rather than using the parm to set it.
4.46.0.6
Window.current_location gets a new parm - more_accurate (defaults to False). If True, uses window's geometry
""" """
__version__ = version.split()[0] # For PEP 396 and PEP 345 __version__ = version.split()[0] # For PEP 396 and PEP 345
@ -9644,19 +9646,33 @@ class Window:
except: except:
pass pass
def current_location(self): def current_location(self, more_accurate=False):
""" """
Get the current location of the window's top left corner. Get the current location of the window's top left corner.
Note that this value may not take into account the titlebar and menubar. Sometimes, depending on the environment, the value returned does not include the titlebar,etc
These parts of a window are created by the OS. As a result, the value returned may be A new option, more_accurate, can be used to get the theoretical upper leftmost corner of the window.
off depending on if your window has a titlebar or menubar. The titlebar and menubar are crated by the OS. It gets really confusing when running in a webpage (repl, trinket)
Thus, the values can appear top be "off" due to the sometimes unpredictable way the location is calculated.
:return: The x and y location in tuple form (x,y) :param more_accurate: If True, will use the window's geometry to get the topmost location with titlebar, menubar taken into account
:rtype: Tuple[(int), (int)] :type more_accurate: (bool)
:return: The x and y location in tuple form (x,y)
:rtype: Tuple[(int | None), (int | None)]
""" """
if not self._is_window_created('tried Window.current_location'): if not self._is_window_created('tried Window.current_location'):
return return (None, None)
return int(self.TKroot.winfo_x()), int(self.TKroot.winfo_y()) try:
if more_accurate:
geometry = self.TKroot.geometry()
location = geometry[geometry.find('+') + 1:].split('+')
x, y = int(location[0]), int(location[1])
else:
x, y = int(self.TKroot.winfo_x()), int(self.TKroot.winfo_y())
except Exception as e:
warnings.warn('Error in Window.current_location. Trouble getting x,y location\n' + str(e), UserWarning)
x, y = (None, None)
return (x,y)
@property @property
def size(self): def size(self):