Search code examples
pythongtkpygtkpygobject

PyGI window not destroying


Here is my class that is representing preferencies. It loads glade layout from 'preferences.glade'. btn_cancel_clicked_cb and btn_ok_clicked_cb are called when the corresponding buttons are activated. But self.destroy() doesn't do anything. Can somebody explain how to destroy this dialog when after clicking the buttons?

from gi.repository import Gtk
from common import Prefs

class ViewPrefs(Gtk.Dialog):
    def __init__(self):
        Gtk.Dialog.__init__(self)
        self.builder = Gtk.Builder()
        self.builder.add_from_file("preferences.glade")
        self.builder.connect_signals(self)

        self.rb_input=self.builder.get_object("rb_input")
        self.rb_select=self.builder.get_object("rb_select")

    def run(self, *args):
        window = self.builder.get_object("window_prefs")
        window.show()
        window.connect('destroy', Gtk.main_quit)
        Gtk.main()

    def register_observer(self, controller):
        self.controller = controller

    def btn_cancel_clicked_cb(self,widget):
        self.destroy()

    def btn_ok_clicked_cb(self,widget):
        active = [r for r in self.rb_input.get_group() if r.get_active()][0]
        input_type=active.get_label().lower()
        self.controller.set_prefs(Prefs(input_type=input_type))
        self.destroy()

It starts from the main window like that:

   prefsview=ViewPrefs()
   prefsview.register_observer(self.controller)
   prefsview.run()

Solution

  • self is not self.window. In fact, subclassing Gtk.Dialog for your case is useless as the dialog part of self is never used! If you are requiring a new enough version of GTK+, you can create your dialog as a composite widget template and build your class that way (I don't know how to do this with Python; sorry). Otherwise, get rid of the subclass and call window.destroy() instead (and, if window is really a Gtk.Dialog, window.run() in your self.run()).