Open In App

wxPython – check items inside Menu

Last Updated : 15 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we are going to learn about check menu item inside Menu in Menubars. We will write a code to show and hide statusbar using Check() function.

Parameters :

Parameter Input Type Description
id int The menu item identifier.
check bool If True, the item will be checked, otherwise it will be unchecked.

Code :




import wx
  
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
  
        self.InitUI()
  
    def InitUI(self):
  
        menubar = wx.MenuBar()
        viewMenu = wx.Menu()
  
        self.showsb = viewMenu.Append(wx.ID_ANY, 'Show statusbar',
                                                 'Show Statusbar'
                                            kind = wx.ITEM_CHECK)
  
        viewMenu.Check(self.showsb.GetId(), True)
   
        self.Bind(wx.EVT_MENU, self.shStatusBar, self.showsb)
  
        menubar.Append(viewMenu, '&View')
        self.SetMenuBar(menubar)
  
        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText('This is statusbar')
  
        self.SetSize((450, 350))
        self.SetTitle('Check menu item')
        self.Centre()
  
  
    def shStatusBar(self, e):
  
        if self.showsb.IsChecked():
            self.statusbar.Show()
        else:
            self.statusbar.Hide()
  
def main():
  
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()


Output :

checked :

unchecked :



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads