Search code examples
gtkpygtktrayicon

pygtk system tray icon doesn't work


i am trying to display system tray icon in my program. when i start my program it shows window and when i colse window it gets hidden. Then if i click on system tray icon it shows me a blank window, but not the contents of window.Why is this happen? Heres the my code:

class Main(gtk.Window):

    def __init__(self):
        super(Main,self).__init__()
        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640,600)
        self.set_geometry_hints(min_width=640,min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        self.connect("destroy",self.window_destroy)
        menubar = self.add_menubar()

        pixbuf = gdk.pixbuf_new_from_file_at_size("../images/activity_monitor2.png",25,25)
        statusicon = gtk.StatusIcon()
        statusicon = gtk.status_icon_new_from_pixbuf(pixbuf)
        statusicon.connect("activate",self.tray_activate)
        self.show_all()

    def tray_activate(self,widget):
        self.show_all()

    def window_destroy(self,widget):
        self.hide_all()

if __name__ == "__main__":
    Main()
    gtk.main()

when i click on system tray icon it shows me window , but a blank window.

So please help me out. Thanks in advanced.


Solution

  • You must handle delete-event to just hide the window and don't destroy that (as by default).

    class Main(gtk.Window):
    
        def __init__(self):
            super(Main, self).__init__()
            self.connect('delete-event', self.on_delete_event)
            self.set_title("Virtual Machine Monitor")
            self.set_position(gtk.WIN_POS_CENTER)
            self.set_default_size(640,600)
            self.set_geometry_hints(min_width=640, min_height=600)
            self.set_icon_from_file("../images/activity_monitor2.png")
            menubar = self.add_menubar()
    
            pixbuf = gdk.pixbuf_new_from_file_at_size("../images/activity_monitor2.png",25,25)
            statusicon = gtk.StatusIcon()
            statusicon = gtk.status_icon_new_from_pixbuf(pixbuf)
            statusicon.connect("activate",self.tray_activate)
            self.show_all()
    
        def on_delete_event(self, widget, event):
            self.hide()
            return True    
    
        def tray_activate(self, widget):
            self.present()
    
    
    if __name__ == "__main__":
        Main()
        gtk.main()