禁用 wxMenuBar
我想在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)
谢谢
1 个回答
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)
这样重新设置一下,菜单栏就会再次出现。其实还有其他更好的方法。
希望这些信息对你有帮助。