Python unity indicator applet and glade child window
Question: I have created an unity indicator applet with python and glade. Here is the
screenshot that appears when indicator applet is clicked. You can see the
preferences menu. When this preferences menu is clicked, it opens a new
window.

`Indicator Applet Menu`  
![Indicator Applet
Menu](http://i76.photobucket.com/albums/j5/alexshr/loadshedding_preference_zps7955b445.png)

`Preference Window`  
![Preference
Window](http://i76.photobucket.com/albums/j5/alexshr/Preference_Loadshedding_zps1421a346.png)

Now the problem is when I click on close button, the whole application exists.

The code that triggers the preference window is as shown below :

    
    
    def action_preferences(self, widget):
        '''
        Show the preferences window
        '''
        base = PreferenceWindow()
        base.main()
    
        self.menu_setup()
    

**preference.py** has the following code :

    
    
    import sys
    import json
    import pynotify
    
    try:
        import pygtk
        pygtk.require("2.0")
    except:
        pass
    try:
        import gtk
        import gtk.glade
    except:
        print("GTK is not Available")
        sys.exit(1)
    
    class PreferenceWindow:
        ui = None
        configs = {}
        notify = None
        window = None
    
        def __init__(self):
    
            if not pynotify.init ("nepal-loadshedding"):
                sys.exit (1)
    
            self.ui = gtk.glade.XML("pref_ui.glade")
    
            # Get the preference saved previously
            self.configs = self.parse_configs()
    
            saved_group_value = str(self.configs.get("GROUP"))
    
            self.ui.get_widget("text_group_number").set_text(saved_group_value)
    
            dic = {
                "on_btn_pref_ok_clicked":self.on_save_preference,
                "on_btn_pref_close_clicked":self.on_close,
                "on_preference_window_destroy":self.on_quit,
                }
    
            self.ui.signal_autoconnect(dic)
    
            if self.window is None:               
                self.window = self.main()
    
        def parse_configs(self):
            self.configs = json.load(open("config.txt"))
            return self.configs
    
        def save_configs(self, key, value):
            self.configs[key] = int(value)
            json.dump(self.configs, open("config.txt", "wb"))
            return True
    
        def on_save_preference(self, widget):
            group_number = self.ui.get_widget("text_group_number").get_text()
            self.save_configs("GROUP", group_number)
    
            # try the icon-summary case
            if self.notify == None:
                self.notify = pynotify.Notification ("Nepal Loadshedding", "Group saved successfully to : " + group_number)
            else:
                self.notify.update("Nepal Loadshedding", "Group saved successfully to : " + group_number)
                self.notify.set_timeout(100)
    
            self.notify.show()
            print "Saved successfully"
    
        def on_close(self, widget):
            print 'close event called'
    
        def on_quit(self, widget):
            sys.exit(0)
    
        def main(self):
            gtk.main()
    


Answer: You can't call `sys.exit()` because it will make your entire application to
terminate (as you can see). What you have to do is call
[`gtk.main_quit()`](http://www.pygtk.org/docs/pygtk/gtk-
functions.html#function-gtk--main-quit). When you click _close_ button, you
can destroy the window.

    
    
    def on_close(self, widget):
        self.ui.get_widget("<window-name>").destroy()
    
    def on_quit(self, widget):
        gtk.main_quit()
    

Additionally, you call two times `PreferencesWindow.main()` (one at the bottom
of `__init__()` and the second time after instantiation, with `base.main()`),
which in turn calls to [`gtk.main()`](http://www.pygtk.org/docs/pygtk/gtk-
functions.html#function-gtk--main). You have to remove one of them or your
application will get stuck on the second one.

