Search code examples
pythonwindowpygobject

How to center a window with PyGObject


I'm currently trying to center my application window, but it seems to be impossible with PyGObject (GTK+ 3). Indeed, with pyGTK, I was doing it this way:

window.set_position(gtk.WIN_POS_CENTER)

So this time I'm trying this:

window.set_position(Gtk.WIN_POST_CENTER)

set_position seems still existing, but the Gtk.WIN_POST_CENTER constant doesn't work:

AttributeError: 'gi.repository.Gtk' object has no attribute 'WIN_POS_CENTER'


Solution

  • pydoc tells you what you need to know:

    >>> help(window.set_position)
    
    Help on method set_position in module gi.repository.Gtk:
    
    set_position(*args, **kwargs) method of gi.overrides.Gtk.Window instance
        set_position(self, position:Gtk.WindowPosition)
    

    Gtk.WindowPosition wraps the enum values, so try:

    window.set_position(Gtk.WindowPosition.CENTER)
    

    edit: seems that for newer versions the docstring isn't displayed by help(window.set_position) anymore, use help(window) (or print(window.get_position.__doc__)) instead.