0
我收到了一本字典,其中的關鍵是菜單,值是菜單項,我想用它來爲我的程序創建MenuBar,但是我在創建菜單時遇到了麻煩,它們沒有按照我的意願出現,如下所示:字典中的wxPython菜單:如何以正確的順序顯示菜單,它出現在字典中?
'文件|編輯|工具|幫助」
這裏是我的代碼:
# -*- coding: utf-8 -*-
__author__ = 'Nilson Lopes'
import wx
class MainFrame(wx.Frame):
__menu_options = {'File': ['New', 'Open', 'SEP', 'Quit'],
'Edit': ['Copy\tCtrl+C', 'Cut\tCtrl+X', 'Paste\tCtrl+V'],
'Tools': ['Settings'],
'Help': ['Context', 'About']}
def __init__(self, *args, **kwargs):
super(MainFrame, self).__init__(*args, **kwargs)
self.create_menu()
'''
def on_new(self, event):
print 'New called from function'
event.Skip()
def on_open(self, event):
print 'Open called from function'
event.Skip()
__actions__ = {'New': on_new(), 'Open': on_open()}
'''
def create_menu(self):
menu_bar = wx.MenuBar()
for option, menu_items in self.__menu_options.items():
#print option
if type(menu_items) is list:
main_menu = wx.Menu()
for menu in self.__menu_options[option]:
#print '\t', menu
if menu == 'SEP':
main_menu.AppendSeparator()
else:
main_menu.Append(wx.NewId(), str(menu))
menu_bar.Append(main_menu, '&'+str(option).capitalize())
self.SetMenuBar(menu_bar)
class MainApp(wx.App):
def OnInit(self):
self.frame = MainFrame(None, title='Main Application')
self.frame.Show()
return True
def app_run():
app = MainApp(False)
app.MainLoop()
if __name__ == '__main__':
app_run()
詳情: 我使用python 2.7使用wxPython的(鳳凰城)在Windows 8
我可以看看你的代碼嗎? @falsetru – noslin005
@ noslin005,我剛剛添加了一個import語句,並替換了'__menu_options'屬性,如答案中所示。 – falsetru
@ noslin005,[這是完整的代碼](http://pastebin.com/EFapXJ8h)。 – falsetru