Tkinter - centered window?

Matthew Dixon Cowles matt at mondoinfo.com
Sat Jun 16 16:57:49 EDT 2001


On 15 Jun 2001 09:59:04 GMT, Jacek Pop³awski <jp at ulgo.koti.com.pl>
wrote:

Jacek,

>I create window this way:

>self.window=Toplevel(parent)

>Is it a way to tell window manager, that I want this window to be in
>center of screen?

Yes, there is. It requires that the window manager cooperate which I
suspect that most will but some won't. You want something like this:

  window=Toplevel(root)
  l=Label(window,text="Hope I'm centered")
  l.pack()
  window.update_idletasks() # Make sure window is mapped
  w=window.winfo_width()
  h=window.winfo_height()
  extraW=window.winfo_screenwidth()-w
  extraH=window.winfo_screenheight()-h
  window.geometry("%dx%d%+d%+d" % (w,h,extraW/2,extraH/2))

Fredrik Lundh's excellent An Introduction to Tkinter at:

http://www.pythonware.com/library/tkinter/introduction/index.htm

gives more details about the calls and the code in the PmwBase module
of Greg McFarlane's handy Pmw has a more sophisticated version. Pmw is
at:

http://pmw.sourceforge.net/

Regards,
Matt



More information about the Python-list mailing list