[Tkinter-discuss] constraining toplevel windows to be on screen?

Russell E. Owen rowen at uw.edu
Thu May 6 21:07:40 CEST 2010


In article <rowen-C1230C.14113604052010 at news.gmane.org>,
 "Russell E. Owen" <rowen at uw.edu> wrote:

> I've received a bug report that my application's windows can appear off 
> screen. This happens when a user has two monitors and saves window 
> positions, then runs again later with only one monitor. Tkinter is 
> apparently perfectly happy to display toplevel windows entirely off 
> screen. At least on Mac OS X.
> 
> The solution I've thought of is to get the full screen size using 
> "root.wm_maxsize" and use that to constrain the geometry when restoring 
> the window positions.
> 
> However, it is messy code because tk geometry strings are complex: they 
> may be missing the extent, or the extent may be positive or negative.
> 
> So I was wondering if somebody had a simpler solution or had already 
> written code they would be willing to share (and, preferably, allow me 
> to redistribute in the RO package).
> 
> It would suffice to show part of each window on screen, though it would 
> be better to show the whole thing.
> 
> Regards,
> 
> -- Russell

OK, I coded up a new class RO.TkUtil.Geometry with a method constrained 
to handle this. Code available at PyPI 
<http://pypi.python.org/pypi?:action=display&name=RO> in case anyone 
else needs this.

In the process of testing this I discovered an amusing Tk 8.4 bug on Mac 
OS X: toplevels with negative corner position (e.g. "200x200-20-20") are 
mis-handled; the corner position is the distance from the bottom right 
corner of the screen to the TOP LEFT corner of the toplevel, instead of 
the bottom right corner.

Anyone out there running Tk 8.5 willing to test if this bug still exists 
on Tk 8.5?

-- Russell



More information about the Tkinter-discuss mailing list