Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

wxPython – Add submenu in Menu

  • Last Updated : 15 May, 2020

In this article we are going to learn How can we add submenu to a Menuitem inside a Menu present on MenuBar. In this we use AppendMenu() function rather than just using Append().

Steps :
1. Create a MenuBar in frame using MenuBar() constructor.
2. Add menu to the menu bar.
3. Create wx.Menu for Menuitem.
4. Add menu using ApendMenu() function.

Syntax :

wx.Menu.AppendMenu(self, id, subMenu, helpString)

Parameters of AppendMenu():

ParameterInput TypeDescription
idintThe menu item identifier.
itemstringthe string to appear on the menu item;
subMenuwx.Menuan instance of FlatMenu, the submenu to append,
helpStringintan optional help string associated with the item. By default, the handler for the EVT_FLAT_MENU_ITEM_MOUSE_OVER event displays this string in the status line.

Code Example:




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()
        fileMenu = wx.Menu()
  
        # submenu for menuitem
        imp = wx.Menu()
        imp.Append(wx.ID_ANY, 'SubMenu 1')
        imp.Append(wx.ID_ANY, 'SubMenu 2')
        imp.Append(wx.ID_ANY, 'SubMenu 3')
  
        # append submenu with menuitem
        fileMenu.AppendMenu(wx.ID_ANY, 'MenuItem', imp)
        menubar.Append(fileMenu, '&Menu')
        self.SetMenuBar(menubar)
  
        self.SetSize((350, 250))
        self.SetTitle('Submenu')
        self.Centre()
  
    def OnQuit(self, e):
        self.Close()
  
  
def main():
  
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()

Output :


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!