import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "多个菜单栏不同菜单界面的切换", size=(800, 600)) self.panel = wx.Panel(self) self.createMenuBar1() self.createMenuBar2() self.createMenuBar3() self.switchMenuBar1() def createMenuBar1(self): self.menuBar1 = wx.MenuBar() menuItem1 = wx.MenuItem(self.menuBar1, -1, '菜单1') self.menuBar1.Append(menuItem1, '菜单1') self.Bind(wx.EVT_MENU, self.onSwitchMenuBar1, menuItem1) self.SetMenuBar(self.menuBar1) self.panel1 = wx.Panel(self, style=wx.BORDER_SUNKEN) self.panel1.SetPosition((0, 25)) self.panel1.SetSize((800, 575)) def createMenuBar2(self): self.menuBar2 = wx.MenuBar() menuItem2 = wx.MenuItem(self.menuBar2, -1, '菜单2') self.menuBar2.Append(menuItem2, '菜单2') self.Bind(wx.EVT_MENU, self.onSwitchMenuBar2, menuItem2) self.panel2 = wx.Panel(self, style=wx.BORDER_SUNKEN) self.panel2.SetPosition((0, 25)) self.panel2.SetSize((800, 575)) def createMenuBar3(self): self.menuBar3 = wx.MenuBar() menuItem3 = wx.MenuItem(self.menuBar3, -1, '菜单3') self.menuBar3.Append(menuItem3, '菜单3') self.Bind(wx.EVT_MENU, self.onSwitchMenuBar3, menuItem3) self.panel3 = wx.Panel(self, style=wx.BORDER_SUNKEN) self.panel3.SetPosition((0, 25)) self.panel3.SetSize((800, 575)) def onSwitchMenuBar1(self, event): self.SetMenuBar(self.menuBar1) self.panel2.Hide() self.panel3.Hide() self.panel1.Show() def onSwitchMenuBar2(self, event): self.SetMenuBar(self.menuBar2) self.panel1.Hide() self.panel3.Hide() self.panel2.Show() def onSwitchMenuBar3(self, event): self.SetMenuBar(self.menuBar3) self.panel1.Hide() self.panel2.Hide() self.panel3.Show() def switchMenuBar1(self): self.SetMenuBar(self.menuBar1) self.panel2.Hide() self.panel3.Hide() self.panel1.Show() if __name__ == '__main__': app = wx.App() frame = MyFrame() frame.Show(True) app.MainLoop()报错TypeError: MenuItem(): argument 1 has unexpected type 'MenuBar'咋解决
时间: 2023-06-07 12:08:16 浏览: 158
wx.CheckBox创建复选框控件并响应鼠标点击事件
这是因为 `menuItem1`, `menuItem2`, `menuItem3` 的第一个参数应该是一个菜单对象,而不是一个菜单栏对象。你可以把它们改为这样:
```python
menuItem1 = wx.MenuItem(self.menuBar1, -1, '菜单1')
```
换句话说,把 `self.menuBar1` 改为对应的菜单对象即可。其他两个菜单也需要做同样的修改。
阅读全文