MacOS 上 wxPython 应用程序的 Dock 问题

发布于 2024-11-30 08:20:45 字数 849 浏览 1 评论 0原文

MacOS 上的扩展坞存在问题。 在停靠图标的上下文菜单中,有 2 个项目:MacOS 的“标准退出”和一些我没有添加的菜单项“退出”。 那个退出,不是我的,是有效的,并且与我的方法相关联:

class TrayIcon(wx.TaskBarIcon):
    def make_menu(self):
        self.menu = wx.Menu()
        item = self.menu.Append(wx.ID_EXIT,"Exit", "Exit from application")
        self.menu.Bind(wx.EVT_MENU, self.on_menu_exit, item)

    def CreatePopupMenu(self):
        self.make_menu()
        return self.menu

如你所见,我将其称为“退出”,但我看到“退出”,但它仍然按我的方法处理。

退出系统,它不起作用,当我选择它时,什么也没有发生,但应用程序的下一步操作会导致此错误消息:

文件 “/var/tmp/wxWidgets/wxWidgets-13~231/2.6/DSTROOT/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode/wx/_core. py", 第 14501 行,在 getattr wx._core.PyDeadObjectError:C++ 部分 Main 对象已被删除,不再允许属性访问。

我做错了什么? 谢谢

There is a problem with dock on MacOS.
In context menu at dock-icon there are 2 items: Standart Quit of MacOS and some menu item Quit, that i didn't add.
That Quit, that not mine, is works and associated with my method:

class TrayIcon(wx.TaskBarIcon):
    def make_menu(self):
        self.menu = wx.Menu()
        item = self.menu.Append(wx.ID_EXIT,"Exit", "Exit from application")
        self.menu.Bind(wx.EVT_MENU, self.on_menu_exit, item)

    def CreatePopupMenu(self):
        self.make_menu()
        return self.menu

As you see, i сall it "Exit", but i see "Quit", but it still handles by my method.

And Quit that is system, it won't work, when i select it, nothing happens, but next actions with application lead to this error message:

File
"/var/tmp/wxWidgets/wxWidgets-13~231/2.6/DSTROOT/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode/wx/_core.py",
line 14501, in getattr wx._core.PyDeadObjectError: The C++ part of
the Main object has been deleted, attribute access no longer allowed.

What did I do wrong?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2024-12-07 08:20:45

原因可能是您没有保留对 item 的引用。它绑定在 self.menu.Bind 中,但您无需在 Python 代码中保留引用。因此,Python 垃圾收集器发现这个 wx.MenuItem 不再被引用(在 Python 代码中),因此将其删除。 Wx 的清理代码 (__del__) 被调用,并且该对象也在 C++ 代码中被删除,尽管它仍在使用中!。因此,当您单击菜单项时,它会尝试将单击的菜单项与(届时)删除的菜单项进行匹配,并抛出上述 PyDeadObjectError。要解决此“错误”,只需保留对 wx.MenuItem 的引用即可:

self.item = self.menu.Append(...

The cause is probably that your are not keeping a reference to item. It is bound in self.menu.Bind, but you do not keep a reference in the Python code. So the Python garbage collector sees this wx.MenuItem that isn't referenced anymore (within the Python code), so it is deleted. Wx's clean up code (__del__) is called and the object is also removed in the C++ code, although it is still in use!. So when you click the menu item, it tries to match the clicked menu item with the (by then) removed menu item and it throws the aforementioned PyDeadObjectError. To work around this 'bug', simply keep a reference to the wx.MenuItem:

self.item = self.menu.Append(...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文