Linux MenuItem位图引发断言失败…;

Linux MenuItem位图引发断言失败…;,linux,windows,python-3.x,wxpython,wxwidgets,Linux,Windows,Python 3.x,Wxpython,Wxwidgets,我正在尝试在首选项菜单项前面创建一个漂亮的齿轮位图。我认为应该这样编码: menubar = wx.MenuBar() fileMenu = wx.Menu() preferences = wx.MenuItem(text="Preferences", helpString="Opens preferences dialog.", kind=wx.ITE

我正在尝试在
首选项
菜单项前面创建一个漂亮的齿轮位图。我认为应该这样编码:

    menubar = wx.MenuBar()
    fileMenu = wx.Menu()
    preferences = wx.MenuItem(text="Preferences",
                              helpString="Opens preferences dialog.",
                              kind=wx.ITEM_NORMAL)
    gear = wx.Bitmap(os.path.join(os.path.dirname(__file__), 'gear.png'))
    preferences.SetBitmap(gear)
    self.shcfg = fileMenu.Append(preferences)
Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps 
然而,这是错误的,因为我得到了一个

Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps 
回溯(最近一次呼叫最后一次):
文件“gui.py”,第193行,在
图形用户界面(无)
文件“gui.py”,第117行,在
self.InitUI()
InitUI中第129行的文件“gui.py”
首选项。SetBitmap(齿轮)
WX.SyrEXECUTION:C++断言“断言失败”在/tMP/PIP-Buudio-ScVD1AJ/WXPython /Ext/WxWistGe/Src/Gtk/Multop.CPP(729)中失败,在StBITMMAP()中:只有普通的菜单项可以有位图

我做错了什么?

您使用的是
Append
而不是
AppendItem

Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps 
我想有很多种方法可以将菜单放在一起,但我发现,所有菜单项都需要Id。
对于预定义的
wx
id,这很简单,因为您可以简单地附加它们,因为它们不仅具有内置id,还具有图像。
对于自定义图像,我使用下面的方法,这一方法一直适用于我。
请注意,我在这个示例代码中使用了预定义Id和自定义Id。
我在下面包含了您代码的一个变体

Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps 
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import os
class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(200, 100))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)

        # Create Statusbar
        self.CreateStatusBar()

        # Set up the menus
        filemenu = wx.Menu()
        infomenu = wx.Menu()

        # file menu
        filemenu.Append(wx.ID_NEW, "New") # Id of wx.ID_NEW (5002) which picks up an automatic image
        filemenu.Append(wx.ID_SAVE, "Save")

        m1 = wx.MenuItem(filemenu, 100, "Manual Bitmap") #A manual id of 100
        m1.SetBitmap(wx.Bitmap('./myimage1.png'))
        filemenu.AppendItem(m1)

        m2 = wx.MenuItem(filemenu, 101, "Manual Bitmap 2") #A manual id of 101
        m2.SetBitmap(wx.Bitmap('./myimage2.png'))
        filemenu.AppendItem(m2)
        #----------------------------------------------#    
        preferences = wx.MenuItem()
        preferences.SetId(102)
        preferences.SetText("Preferences")
        preferences.SetHelp("Preferences Help")
        preferences.SetKind(wx.ITEM_NORMAL)
        gear = wx.Bitmap(os.path.join(os.path.dirname(__file__), 'myimage2.png'))
        preferences.SetBitmap(gear)
        filemenu.AppendItem(preferences)
        #----------------------------------------------#    
        filemenu.AppendSeparator()

        filemenu.Append(wx.ID_EXIT, "Exit")

        # info menu
        infomenu.Append(wx.ID_ABOUT, "About")

        # bind file menu
        self.Bind(wx.EVT_MENU, self.OnManualBitmap, id=100) # Bind to the Id
        self.Bind(wx.EVT_MENU, self.OnManualBitmap, id=101) # Bind to the Id
        self.Bind(wx.EVT_MENU, self.OnManualBitmap, id=102) # Bind to the Id
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)

        # Creating the menubar.
        menuBar = wx.MenuBar()

        # Add menus
        menuBar.Append(filemenu, "&Preferences")
        menuBar.Append(infomenu, "&Help")

        # Add the MenuBar to the Frame content.
        self.SetMenuBar(menuBar)
        self.Show(True)

    def OnManualBitmap(self, event):
        print event.EventObject.GetLabel(event.Id)
        print event.Id

    def OnExit(self, event):
        self.Destroy()

app = wx.App()
frame = MainWindow(None, "Menu Image Test")
app.MainLoop()

您使用的是
Append
而不是
AppendItem

Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps 
我想有很多种方法可以将菜单放在一起,但我发现,所有菜单项都需要Id。
对于预定义的
wx
id,这很简单,因为您可以简单地附加它们,因为它们不仅具有内置id,还具有图像。
对于自定义图像,我使用下面的方法,这一方法一直适用于我。
请注意,我在这个示例代码中使用了预定义Id和自定义Id。
我在下面包含了您代码的一个变体

Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps 
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import os
class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(200, 100))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)

        # Create Statusbar
        self.CreateStatusBar()

        # Set up the menus
        filemenu = wx.Menu()
        infomenu = wx.Menu()

        # file menu
        filemenu.Append(wx.ID_NEW, "New") # Id of wx.ID_NEW (5002) which picks up an automatic image
        filemenu.Append(wx.ID_SAVE, "Save")

        m1 = wx.MenuItem(filemenu, 100, "Manual Bitmap") #A manual id of 100
        m1.SetBitmap(wx.Bitmap('./myimage1.png'))
        filemenu.AppendItem(m1)

        m2 = wx.MenuItem(filemenu, 101, "Manual Bitmap 2") #A manual id of 101
        m2.SetBitmap(wx.Bitmap('./myimage2.png'))
        filemenu.AppendItem(m2)
        #----------------------------------------------#    
        preferences = wx.MenuItem()
        preferences.SetId(102)
        preferences.SetText("Preferences")
        preferences.SetHelp("Preferences Help")
        preferences.SetKind(wx.ITEM_NORMAL)
        gear = wx.Bitmap(os.path.join(os.path.dirname(__file__), 'myimage2.png'))
        preferences.SetBitmap(gear)
        filemenu.AppendItem(preferences)
        #----------------------------------------------#    
        filemenu.AppendSeparator()

        filemenu.Append(wx.ID_EXIT, "Exit")

        # info menu
        infomenu.Append(wx.ID_ABOUT, "About")

        # bind file menu
        self.Bind(wx.EVT_MENU, self.OnManualBitmap, id=100) # Bind to the Id
        self.Bind(wx.EVT_MENU, self.OnManualBitmap, id=101) # Bind to the Id
        self.Bind(wx.EVT_MENU, self.OnManualBitmap, id=102) # Bind to the Id
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)

        # Creating the menubar.
        menuBar = wx.MenuBar()

        # Add menus
        menuBar.Append(filemenu, "&Preferences")
        menuBar.Append(infomenu, "&Help")

        # Add the MenuBar to the Frame content.
        self.SetMenuBar(menuBar)
        self.Show(True)

    def OnManualBitmap(self, event):
        print event.EventObject.GetLabel(event.Id)
        print event.Id

    def OnExit(self, event):
        self.Destroy()

app = wx.App()
frame = MainWindow(None, "Menu Image Test")
app.MainLoop()

您是否尝试过省略
kind=wx.ITEM\u NORMAL
?看起来wxPython可能处理不当,导致创建一个可检查项。至少,根据断言消息,这就是最终发生的情况。

您是否尝试过省略
kind=wx.ITEM\u NORMAL
?看起来wxPython可能处理不当,导致创建一个可检查项。至少,根据断言消息,这就是最终发生的情况。

引发了
AttributeError:“MenuItem”对象没有属性“SetId”
…我运行的是Python 3,而不是2。对代码进行黑客攻击以使其进行编译会引发与问题中相同的错误。☹您不能使用python3运行wxpython(经典)。如果你正在运行wxpython phoenix,据我所知,它还没有出现!我的答案是使用wxpython3.0.2.0gtk2(经典版)和python2.7.12,我想这都在版本中;)我在运行wxpython phoenix。所以我想我不能这么做。不用担心。您可以绑定到menuitem名称而不是
id
,因此在本例中,
preferences
,但对于wx classic,我测试中的
id
以这种方式绑定的所有项目都返回了-2,不太令人满意。Phoenix可能会有所不同,尽管我仍然找不到它的文档,我想,在某个时候,我将不得不迁移到wx Phoenix的共享软件项目主机。Phoenix仍然在阿尔法/测试版,所以我现在不会太努力。它处于不断变化的状态。引发
AttributeError:'MenuItem'对象没有属性“SetId”
…我运行的是Python 3,而不是2。对代码进行黑客攻击以使其进行编译会引发与问题中相同的错误。☹您不能使用python3运行wxpython(经典)。如果你正在运行wxpython phoenix,据我所知,它还没有出现!我的答案是使用wxpython3.0.2.0gtk2(经典版)和python2.7.12,我想这都在版本中;)我在运行wxpython phoenix。所以我想我不能这么做。不用担心。您可以绑定到menuitem名称而不是
id
,因此在本例中,
preferences
,但对于wx classic,我测试中的
id
以这种方式绑定的所有项目都返回了-2,不太令人满意。Phoenix可能会有所不同,尽管我仍然找不到它的文档,我想,在某个时候,我将不得不迁移到wx Phoenix的共享软件项目主机。Phoenix仍然在阿尔法/测试版,所以我现在不会太努力。它处于不断变化的状态。
Traceback (most recent call last):                                              
  File "gui.py", line 193, in <module>                                          
    GUI(None)                                                                   
  File "gui.py", line 117, in __init__                                          
    self.InitUI()                                                               
  File "gui.py", line 129, in InitUI                                            
    preferences.SetBitmap(gear)                                                 
wx._core.wxAssertionError: C++ assertion "Assert failure" failed at /tmp/pip-build-sc_vd1aj/wxPython/ext/wxWidgets/src/gtk/menu.cpp(729) in SetBitmap(): only normal menu items can have bitmaps