2017-07-05 30 views
0

我想要在Preferences菜单项前面有一个漂亮的齿轮位图。这是我认为它应该被编码:MenuItem位图引发断言失败...

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 

我在做什么错?

回答

1

您使用Append而不是AppendItem

我假设有任何数量的方式把一个菜单在一起,但我发现,所有菜单项要求的ID。
对于预定义的wx id的它是直截了当的,因为你可以简单地追加它们,因为它们不仅具有内置Id而且还具有图像。
对于自定义图像,我使用下面的方法,它一直为我工作。
请注意,我已在此示例代码中使用了预定义的ID和自定义ID。
我已经包括了你的代码的下方

#!/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() 
+0

引发'AttributeError:'MenuItem'对象没有属性'SetId'' ...我正在运行Python 3,而不是2。对代码进行黑客处理以便编译引发与问题中相同的错误。 ☹ – Sardathrion

+1

您不能使用python3运行wxpython(经典)。如果你正在运行wxpython phoenix,据我所知,它仍然不存在!我的答案愉快地运行使用wxpython 3.0.2.0 gtk2(经典)与Python 2.7.12我想这是所有的版本;) –

+0

我正在运行wxpython phoenix。所以我想我不能那样做。别担心。 – Sardathrion

1

你试过省略kind=wx.ITEM_NORMAL一个变种?看起来它可能被wxPython处理不当,导致创建一个可选项目。根据断言消息,至少这种情况最终会发生。

+0

是的,我尝试过,但得到了同样的错误。 – Sardathrion