User
User

Reputation: 24759

Disable wxMenuBar

So I'm looking to disable and enable a wxMenuBar in wxPython. Basically, grey out the whole thing.

If you look at the documentation: http://docs.wxwidgets.org/trunk/classwx_menu_bar.html ... you can see that the enable function takes a parameter for the menu item. As in, it doesn't disable/enable the whole menu, just a certain item.

Better yet, there's an EnableTop(size_t pos, bool enable) function to disable a whole menu, but not the whole bar.

Do I have to disable each item or menu individually? There's no function for doing the whole bar?

I made a function to do this manually but there must be a better way?

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

Thanks

Upvotes: 0

Views: 551

Answers (1)

ρss
ρss

Reputation: 5315

You can disable the whole Menu by using EnableTop()

Code sample:

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()

menu

Also if you use self.SetMenuBar(None) the whole menu bar is gone as shown below. You can toggle the showing/hiding of the menu bar using this quick and dirty way. To show the menu bar again just set it again like self.SetMenuBar(menuBar) then the menu bar will be visible again. There could be a better approach too.

nomenu

I hope it was helpful.

Upvotes: 1

Related Questions