2

Since I don't have much reputation. The original question has been posted in stackoverflow and here is the link:
Python unity indicator applet and glade child window

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

Preference Window
Preference Window

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()

1 Answer 1

3
            "on_preference_window_destroy":self.on_quit,

This line is your problem. When the window is destroyed, you're calling a quit method. When you close the window, it gets destroyed, thus resulting in your quit method being called.

Also, you don't want to call sys.exit(0) to exit a GTK+ application. You want to use Gtk.main_quit() instead. It would also be a good idea to convert your application to using the gobject-introspection bindings for GTK+ and related libraries, rather than using the old and deprecated pygtk bindings.

5
  • Very useful page for porting: live.gnome.org/PyGObject
    – Timo
    May 23, 2013 at 14:04
  • Thank you for quick response, I will try it and let you know the result. And yeah thanks for the Gtk.main_quit(). I knew it exists but wasn't sure of which one to use. @Timo Thanks for link. I am thinking to port this application to the latest version of python and make use of gobject-introspection. This application is also available in github github.com/samundra/Nepal-Loadshedding-Indicater
    – Samundra
    May 24, 2013 at 6:03
  • I commented out the line python #"on_preference_window_destroy":self.on_quit and changed all exit to gtk.main_quit() Now when i click on the close button, Nothing happens. But as I click close button 3 times, the whole application exits. It seems like the opening of preference window is chaining events. but when I close the whole preference window by click x button and again open it and again try to close by clicking close button then this time I have to click 5 times and whole application exits.
    – Samundra
    May 24, 2013 at 6:10
  • @Samundra: your handlers are still wrong, commenting the line is not going to solve the greater issue. You should have the same callback function for both on_btn_pref_close_clicked and on_preference_window_destroy. If you always build that preference dialog again, the callback should do self.dialog.destroy(); return False or when you keep it around, do self.dialog.hide(); return True.
    – Timo
    May 24, 2013 at 9:13
  • Thanks for your effort. Seems like I have to rebuild the whole application using the latest py3 and gtk bindings. Since I have messed and would like to have a fresh starting. When I try to use the self.dialog.destroy() I don't have dialog window. Preference window is called as python def action_preferences(self, widget): base = PreferenceWindow() base.main() the way it has been called makes even harder to get the state of the window, or I might be doing it completely wrong, which I am sure of. Please guide me to the correct direction. Thank you again.@peter
    – Samundra
    May 27, 2013 at 4:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .