FAQ Index - Search - Recent Changes - Everything - Add entry

RCS/faq10.003.htp,v  -->  standard output
revision 1.6
Title: 10.3. How do I get my windows to show up where I want?
Last-Changed-Date: Thu Sep 23 00:45:24 2004
Last-Changed-Author: Christian Robottom Reis
Last-Changed-Email: kiko@async.com.br
Last-Changed-Remote-Host: manonegra.async.com.br
Last-Changed-Remote-Address: 192.168.99.6

The short answer is: you don't; in X11, the window manager usually decides where the window is going to show up. However, you can provide hints so it has an idea of where the best place might be. Matt Wilson says:

 The window manager is responsible for placing the window on the screen
 when mapped if it isn't given an exact position.  For dialog boxes, 
 make sure you're passing in the parent window when creating the dialog
 box.  Then GTK will set up the correct hints that would make window
 managers treat the dialog window as a child of its parent.  How the
 window manager decides to treat the placement is configurable in some
 window managers...

If you would like to specify that a dialog is a `sub-window' of a window, for instance, you can use the following commands:

 w = gtk.Window()
 # ...
 d = gtk.Dialog()
 d.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
 d.set_transient_for(w)

When d is displayed, the window manager will know it is a transient for the parent window. By calling set_position it will [usually] be placed centered upon the parent.

To do this with a dialog defined in a glade file, you'll need to do something like:

 d = glade_tree.get_widget(dialog_name)
 d.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
 d.set_transient_for(parent_window)

If you want to specify positioning further -- to get a centered dialog, for instance -- you can use other options to the GtkWindow.set_position() method, which is described at http://www.moeraki.com/pygtkreference/pygtk2reference/class-gtkwindow.html#method-gtkwindow--set-position

You can also save and restore the window position and size. Save the position with:

    (x, y) = w.get_position()
    (w, h) = w.get_size()

Later restoring it with:

    w = gtk.GtkWindow()
    w.move(x, y)
    w.set_size_request(w, h)

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org