2017-07-05 27 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