Open In App

wxPython - Add submenu in Menu

Last Updated : 15 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Python3
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 :


Next Article
Practice Tags :

Similar Reads