【问题标题】:wxPython build menus on MacBookwxPython 在 MacBook 上构建菜单
【发布时间】:2019-09-16 05:44:11
【问题描述】:

在运行 MacOS Mojave 10.14.6、Python 3.7.4 和 wxPython 4.0.6 的 MacBook 上,我在创建 GUI 时无法使用菜单。这是我正在使用的代码。

def basicGUI(self):
    menuBar = wx.MenuBar()
    fileButton = wx.Menu()
    exitItem = fileButton.Append(wx.ID_EXIT, 'Exit', 'status msg..')

    menuBar.Append(fileButton, 'File')
    menuBar.Append(fileButton, 'Edit')

    self.SetMenuBar(menuBar)

    self.Bind(wx.EVT_MENU, self.Quit, exitItem)

    self.SetTitle('Epic Window')
    self.Show(True)

创建框架和面板就可以了。任何援助将不胜感激。谢谢

【问题讨论】:

    标签: macos wxpython


    【解决方案1】:

    我没有 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()
    

    旁注: 如果您发布问题的可运行示例而不仅仅是摘录的方法,这将很有帮助,这样我们就可以在完整的上下文中看到问题,而不必假设程序的其余部分是什么样子。

    【讨论】:

    • 您好,感谢您的建议。唉,它没有用。我得到的只是框架,没有菜单或按钮。
    • @GrahamHansen 我重新格式化了代码以显式创建菜单项,看看是否可行
    • 不行,结果一样。无法将帧的屏幕截图放入此编辑(sri)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多