【问题标题】:Disable wxMenuBar禁用 wxMenuBar
【发布时间】:2014-04-19 01:41:54
【问题描述】:

所以我希望在 wxPython 中禁用和启用 wxMenuBar。基本上,整个事情都是灰色的。

如果您查看文档:http://docs.wxwidgets.org/trunk/classwx_menu_bar.html ...您可以看到启用功能为菜单项提供了一个参数。例如,它不会禁用/启用整个菜单,只是某个项目。

更好的是,有一个 EnableTop(size_t pos, bool enable) 函数可以禁用整个菜单,但不能禁用整个栏。

我必须单独禁用每个项目或菜单吗?没有做整条的功能?

我创建了一个手动执行此操作的函数,但一定有更好的方法吗?

def enableMenuBar(action): #true or false
    for index in range(frame.menuBar.GetMenuCount()):
        frame.menuBar.EnableTop(index, action)

谢谢

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    您可以使用 EnableTop() 禁用整个菜单

    代码示例:

    import wx
    
    class gui(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
            menuBar = wx.MenuBar()
            file = wx.Menu()
            quit = wx.MenuItem(file, 101, '&Quit\tCtrl+Q', 'Quit the Application')
            about = wx.MenuItem(file, 102, '&About\tCtrl+A', 'About the  Application')
            help = wx.MenuItem(file, 103, '&Help\tCtrl+H', 'Help related to the Application')
            file.AppendItem(help)
            file.AppendSeparator()
            file.AppendItem(about)
            file.AppendSeparator()
            file.AppendItem(quit)
            file.AppendSeparator()
            menuBar.Append(file, '&File')
            self.SetMenuBar(menuBar)
            menuBar.EnableTop(0, False)#Comment out this to enable the menu
            #self.SetMenuBar(None)#Uncomment this to hide the menu bar
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = gui(parent=None, id=-1, title="My-App")
        frame.Show()
        app.MainLoop()
    

    另外,如果你使用self.SetMenuBar(None),整个菜单栏就会消失,如下所示。您可以使用这种快速而肮脏的方式切换菜单栏的显示/隐藏。要再次显示菜单栏,只需像self.SetMenuBar(menuBar) 一样再次设置它,然后菜单栏将再次可见。也可能有更好的方法。

    希望对你有帮助。

    【讨论】:

    • 谢谢,但您的代码只禁用了一个菜单,而不是整个 menuBar。
    • 我认为您可以显示或隐藏菜单栏。禁用菜单栏的真正含义是什么?当您禁用所有菜单时,菜单栏看起来像已禁用,因为它不会响应鼠标单击。如我的代码所示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2020-09-18
    相关资源
    最近更新 更多