Search code examples
pythonlinuxgtkglade

Have a popup window stay centered on parent - Glade, GTK, Python


Can anyone tell me how I can have a preference pop-up stay centered on parent window even while moving the parent window? Thank you :)


Solution

  • Here is a very simple example, the preference window is centered and while moving it the parent will also move (but will depend on window manager):

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    class AppWindow(Gtk.Window):
    
        def __init__(self):
            Gtk.Window.__init__(self, title="Python Example")
    
            box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,spacing=6)
            self.add(box)
    
            button1 = Gtk.Button("Information")
            button1.connect("clicked", self.on_button_clicked)
            box.add(button1)
    
            treeview = Gtk.TreeView()
            treeview.set_size_request(640,480)
            box.add(treeview)
    
        def on_button_clicked(self, widget):
            preferences_window = Gtk.Window (Gtk.WindowType.TOPLEVEL)
            preferences_window.set_title ("Preferences Window")
            preferences_window.set_modal (True)
            preferences_window.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
            preferences_window.set_transient_for(self)
            preferences_window.show_all()
            preferences_window.connect("delete-event", preferences_window.destroy);
    
    win = AppWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()
    

    Resulting in: enter image description here