Search code examples
pythonpygtkpygobject

Gtk.StatusIcon PopupMenu in python


im trying to port some small examples from PyGTK to the new PyGobject bindings, but ive hit a roadblock with a popupmenu, despite getting no errors, no menu is being shown on rightclick, here is the code,

from gi.repository import Gtk
class aStatusIcon:
    def __init__(self):
        self.statusicon = Gtk.StatusIcon()
        self.statusicon.set_from_stock(Gtk.STOCK_HOME) 
        self.statusicon.connect("popup-menu", self.right_click_event)

        window = Gtk.Window()
        window.connect("destroy", lambda w: Gtk.main_quit())
        window.show_all()

    def right_click_event(self, icon, button, time):
        menu = Gtk.Menu()

        about = Gtk.MenuItem()
        about.set_label("About")
        quit = Gtk.MenuItem()
        quit.set_label("Quit")

        about.connect("activate", self.show_about_dialog)
        quit.connect("activate", Gtk.main_quit)

        menu.append(about)
        menu.append(quit)

        menu.show_all()

        #menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusicon) # previous working pygtk line
        menu.popup(None, None, None, Gtk.StatusIcon.position_menu, button, time) #i assume this is problem line

    def show_about_dialog(self, widget):
        about_dialog = Gtk.AboutDialog()

        about_dialog.set_destroy_with_parent(True)
        about_dialog.set_name("StatusIcon Example")
        about_dialog.set_version("1.0")
        about_dialog.set_authors(["Andrew Steele"])

        about_dialog.run()
        about_dialog.destroy()

aStatusIcon()
Gtk.main()

i assume the problem is im not telling the menu about self.statusicon in there, but it doesnt work in any of the args since they all want a widget arg or none, not a statusicon, any smart ppl here got an idea where im going wrong?


Solution

  • ah finally, if anyone else has this problem, it got solved thanks to some awesome help from one of the guys on gimpnet#python youve got to keep your menu in scope or it gets garbage collected hence no errors but no menu either this is the working code

    from gi.repository import Gtk
    
    class aStatusIcon:
        def __init__(self):
            self.statusicon = Gtk.StatusIcon()
            self.statusicon.set_from_stock(Gtk.STOCK_HOME)
            self.statusicon.connect("popup-menu", self.right_click_event)
    
            window = Gtk.Window()
            window.connect("destroy", lambda w: Gtk.main_quit())
            window.show_all()
    
        def right_click_event(self, icon, button, time):
            self.menu = Gtk.Menu()
    
            about = Gtk.MenuItem()
            about.set_label("About")
            quit = Gtk.MenuItem()
            quit.set_label("Quit")
    
            about.connect("activate", self.show_about_dialog)
            quit.connect("activate", Gtk.main_quit)
    
            self.menu.append(about)
            self.menu.append(quit)
    
            self.menu.show_all()
    
            def pos(menu, icon):
                    return (Gtk.StatusIcon.position_menu(menu, icon))
    
            self.menu.popup(None, None, pos, self.statusicon, button, time)
    
        def show_about_dialog(self, widget):
            about_dialog = Gtk.AboutDialog()
    
            about_dialog.set_destroy_with_parent(True)
            about_dialog.set_name("StatusIcon Example")
            about_dialog.set_version("1.0")
            about_dialog.set_authors(["Andrew Steele"])
    
            about_dialog.run()
            about_dialog.destroy()
    
    aStatusIcon()
    Gtk.main()