指针悬停在菜单上后,Gtk MenuBar会杀死MenuItems

时间:2023-01-24 15:06:18

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.

我在下面有一个简单的gtk.MenuBar框架。每当鼠标悬停在某个项目上,并且该子菜单由于失去焦点而被隐藏时,在重新访问该子菜单时,所有项目都将丢失。

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?

这个感知错误的原因是什么? gtk.MenuBar是否需要更多“实现”才能正常运行?

The undesired result is illustrated below:

不良结果如下所示:

指针悬停在菜单上后,Gtk MenuBar会杀死MenuItems指针悬停在菜单上后,Gtk MenuBar会杀死MenuItems

#!/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

2 个解决方案

#1


0  

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.

我不太了解PyGTK,但我怀疑它创建的Python对象与GTK菜单项对象/结构相对应。这些Python对象可能会在构造函数的末尾收集垃圾,当它们被垃圾收集时,它们可能会删除底层的GTK C / C ++对象。我怀疑菜单项目正在消失,因为它们已被删除。

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

通过存储Python对象,可以防止它们被垃圾收集,这样可以防止相应的C / C ++对象被删除。

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

然而,这是猜测;我没有测试你的代码来验证这个建议是否有效,所以我可能完全错了。

#2


0  

The error is likely due to having several pygtk all-in-one bundles installed simultaneously.

该错误可能是由于同时安装了几个pygtk all-in-one软件包。

e.g

pygtk-all-in-one-2.24.2.win32-py2.7.msi
(and)
pygtk-all-in-one-2.22.6.win32-py2.7.msi

To fix the issue, uninstall all versions using the installer or the control panel and install only one.

要解决此问题,请使用安装程序或控制面板卸载所有版本,并仅安装一个版本。

#1


0  

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.

我不太了解PyGTK,但我怀疑它创建的Python对象与GTK菜单项对象/结构相对应。这些Python对象可能会在构造函数的末尾收集垃圾,当它们被垃圾收集时,它们可能会删除底层的GTK C / C ++对象。我怀疑菜单项目正在消失,因为它们已被删除。

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

通过存储Python对象,可以防止它们被垃圾收集,这样可以防止相应的C / C ++对象被删除。

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

然而,这是猜测;我没有测试你的代码来验证这个建议是否有效,所以我可能完全错了。

#2


0  

The error is likely due to having several pygtk all-in-one bundles installed simultaneously.

该错误可能是由于同时安装了几个pygtk all-in-one软件包。

e.g

pygtk-all-in-one-2.24.2.win32-py2.7.msi
(and)
pygtk-all-in-one-2.22.6.win32-py2.7.msi

To fix the issue, uninstall all versions using the installer or the control panel and install only one.

要解决此问题,请使用安装程序或控制面板卸载所有版本,并仅安装一个版本。