TK: Ho to set window position

Eric Brunel eric_brunel at despammed.com
Thu Oct 28 05:30:39 EDT 2004


andrea valle wrote:
> Ok, I know it's basic, but I cannot find a reference...
> How can I set the position on the screen of a frame in Tkinter?

First, according to Tkinter terminology, a frame is not a window. A window is 
called a Toplevel in Tkinter (I see a lot of examples building Tkinter 
applications by inheriting from Frame; don't do that! Or you will have big 
problems when trying to do things on the actual window, i.e. the Toplevel 
instance. If you really want to sub-class something, sub-class Toplevel or Tk 
for the main window)

So, once you have your Toplevel instance, the method positionning/sizing the 
window is calledgeometry, to which you pass a string formatted like 
'[WxH][+X+Y]', where W and H are the width and height you want for your window, 
and X and Y its coordinates. Example:

 >>> from Tkinter import *
 >>> root = Tk()
 >>> root.geometry('200x150')
''
 >>> root.geometry('+10+10')
''
 >>> root.geometry('300x200+40+80')
''

The first root.geometry sets the window's dimensions only, the second one its 
position only, and the third one its dimensions and position.

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list