wxPython – Add submenu in Menu
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():
Parameter Input Type Description id int The menu item identifier. item string the string to appear on the menu item; subMenu wx.Menu an instance of FlatMenu, the submenu to append, helpString int an 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 :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.