Gtk MenuBar kills MenuItems after the pointer hovers over them
Question: I have a plain `gtk.MenuBar` frame below. Whenever the mouse hovers over an
item, and that submenu is hidden due to lost focus, upon revisiting that
submenu, all items are lost.

The expected behavior is that the menu items re-appear in the order they were
added to their menus and submenus each time 'File', 'Game' and 'Help' are
activated with the pointer.

What is the cause of this perceived bug? Does the `gtk.MenuBar` require more
"implementing" to function properly?

The undesired result is illustrated below:

![pre-hover](http://i.stack.imgur.com/jZgz3.png) ![post-
hover](http://i.stack.imgur.com/zELra.png)

    
    
    #!/usr/bin/python
    import gtk
    
    def callbackz(*argv):
        print str(*argv)
    
    class ChessMenuBar(gtk.MenuBar):
        def __init__(self):
            super(ChessMenuBar, self).__init__()
    
            menu_file = gtk.Menu()
    
            item_open = gtk.MenuItem('Open')
            item_save = gtk.MenuItem('Save')
            item_quit = gtk.MenuItem('Quit')
            menu_file.append(item_open)
            menu_file.append(item_save)
            menu_file.append(item_quit)
    
            menu_game = gtk.Menu()
            item_newg = gtk.MenuItem('Star New Game')
            menu_game.append(item_newg)
    
            menu_help = gtk.Menu()
            item_help = gtk.MenuItem('Help')
            menu_help.append(item_help)
    
            # --  main categories
            item_file = gtk.MenuItem('File')
            item_game = gtk.MenuItem('Game')
            item_help = gtk.MenuItem('Help')
    
            item_file.set_submenu(menu_file)
            item_game.set_submenu(menu_game)
            item_help.set_submenu(menu_help)
    
            self.append(item_file)
            self.append(item_game)
            self.append(item_help)
    
    
            for _ in [ item_file, item_game, item_help,
                    item_help, item_newg, item_open, 
                    item_quit, item_save]:
                _.connect('activate', callbackz)
    
    class PyApp(gtk.Window):
    
        def __init__(self):
            super(PyApp, self).__init__()
    
            self.set_title("Simple menu")
            self.set_size_request(250, 200)
            self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
            self.set_position(gtk.WIN_POS_CENTER)
    
            mb = ChessMenuBar()
    
            vbox = gtk.VBox(False, 2)
            vbox.pack_start(mb, False, False, 0)
    
            self.add(vbox)
    
            self.connect("destroy", gtk.main_quit)
            self.show_all()
    
    
    PyApp()
    gtk.main()
    

* * *
    
    
    $ winPython --version
    Python 2.7.3
    
    $ cat /cygdrive/c/Python27/Lib/site-packages/gtk-2.0/pygtk-2.22.0-py2.7.egg-info
    Metadata-Version: 1.1
    Name: pygtk
    Version: 2.22.0
    ( abbreviated )
    Platform: MS Windows
    Requires: pycairo (>=1.0.2)
    Requires: pygobject (>=2.21.3)
    Provides: p
    Provides: y
    Provides: g
    Provides: t
    Provides: k
    


Answer: Does it help if you store the menu items somewhere, e.g. in `self`, by
replacing

    
    
    item_open = gtk.MenuItem('Open')
    

with

    
    
    self.item_open = gtk.MenuItem('Open')
    

(and similarly for the other menu items)?

I don't know PyGTK that well, but I suspect that it creates Python objects
that correspond to GTK menu-item objects/structures. These Python objects
might be getting garbage collected at the end of the constructor, and when
they get garbage collected, they might delete the underlying GTK C/C++ object.
My suspicions are that the menu items are disappearing because they are
getting deleted.

By storing the Python objects, you're preventing them from being garbage
collected, which should prevent the corresponding C/C++ objects from being
deleted.

This is speculation, however; I haven't tested your code to verify that this
suggestion works, so I could be completely wrong.

