[Tkinter] Centering a window in the screen ?

Jerome Quelin jerome.quelin at insalien.org
Wed Jun 28 13:59:54 EDT 2000


Hi all,

When using Tkinter, how to center a window in the screen? I tried the
following:

    def center_toplevel_in_screen(widget):
        top = widget.winfo_toplevel()
        sw = top.winfo_screenwidth()
        sh = top.winfo_screenheight()
        w = top.winfo_width()
        h = top.winfo_height()
        x = (sw - w)/2
        y = (sh - h)/2
        geom = '%dx%d+%d+%d' % (w, h, x, y)
        top.geometry(geom)

but this doesn't work: it centers the window but the width and height
returned by the functions are equal to 1.

So, here are some questions:
- Is there a "center-window" in the Tkinter module?
- Which functions am I to call to get the size of a window / a widget?
- What are all those winfo_* functions? Are we supposed to use them or
  should we use some more user-oriented functions (such as geometry(),
  configure(), etc.)?
- By the way, what are those tuples returned by widget.configure()?
- Also, is there a Tkinter FAQ? I went on www.python.org, but there
  isn't much information on Tkinter... Where can I find some (and if
  possible in a downloadable form)?

Thanks,
Jerome
--
jerome.quelin at insalien.org





More information about the Python-list mailing list