wxPython – Add submenu in Menu
Last Updated :
15 Mar, 2023
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():
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:
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()
imp = wx.Menu()
imp.Append(wx.ID_ANY, 'SubMenu 1' )
imp.Append(wx.ID_ANY, 'SubMenu 2' )
imp.Append(wx.ID_ANY, 'SubMenu 3' )
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 :
Share your thoughts in the comments
Please Login to comment...