我没有 Mac,但在 Windows 上运行代码会产生断言错误,因为您添加了两次名为“fileButton”的菜单。如果您注释掉 menuBar.Append(fileButton, 'Edit') 行,您的示例应该运行。如果要创建编辑菜单,不要重复使用文件菜单实例,创建一个新的wx.Menu() 实例。
import wx
class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.basicGUI()
def basicGUI(self):
menuBar = wx.MenuBar()
fileButton = wx.Menu()
editmenu = wx.Menu()
exitItem = wx.MenuItem(fileButton, wx.ID_EXIT, "Exit")
edit_item = wx.MenuItem(editmenu, wx.ID_EDIT, "Edit")
fileButton.Append(exitItem)
editmenu.Append(edit_item)
menuBar.Append(fileButton, 'File')
menuBar.Append(editmenu, 'Edit')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.Quit, id=wx.ID_EXIT)
self.Bind(wx.EVT_MENU, self.on_edit, id=wx.ID_EDIT)
self.SetTitle('Epic Window')
self.CenterOnScreen(wx.BOTH)
self.Show(True)
def Quit(self, event):
self.Close()
def on_edit(self, event):
with wx.MessageDialog(self, "You clicked edit", "Caption", wx.ICON_INFORMATION) as dialog:
dialog.ShowModal()
app = wx.App()
frame = Frame(parent=None)
app.MainLoop()
旁注:
如果您发布问题的可运行示例而不仅仅是摘录的方法,这将很有帮助,这样我们就可以在完整的上下文中看到问题,而不必假设程序的其余部分是什么样子。