Menu Item, Menu & MenuBar



A horizontal bar just below the title bar of a top level window is reserved to display a series of menus. It is an object of wx.MenuBar class in wxPython API.

An object of wx.Menu class is added to the menu bar. It is also used to create context menu and popup menu. Each menu may contain one or more wx.MenuItem objects or cascaded Menu objects.

wx.MenuBar class has a parameterized constructor in addition to a default one.

wx.MenuBar()  

wx.MenuBar(n, menus, titles, style)

Parameter ‘n’ represents the number of menus. Menu is an array of menus and titles, and an array of strings. If the style parameter is set to wx.MB_DOCKABLE, the menubar can be docked.

Following is a list of methods of wx.MenuBar class −

S.N. Methods & Description
1

Append()

Adds menu object to bar

2

Check()

Checks or unchecks a menu

3

Enable()

Enables or disables menu

4

Remove()

Remove a menu from bar

A wx.Menu class object is a pull-down list of one or more menu items, one of which may be selected by the user.

The following table shows frequently required methods of wx.Menu class −

S.N. Methods & Description
1

Append()

Adds a menu item in the menu

2

AppendMenu()

Appends a sub menu

3

AppendRadioItem()

Appends a selectable radio item

4

AppendCheckItem()

Appends a checkable menu item

5

AppendSeparator()

Adds a separator line

6

Insert()

Inserts a new menu at the given position

7

InsertRadioItem()

Inserts a radio item at the given position

8

InsertCheckItem()

Inserts a new check item at the given position

9

InsertSeparator()

Inserts a separator line

10

Remove()

Removes an item from the menu

11

GetMenuItems()

Returns a list of menu items

A Menu Item can be directly added using Append() function, or an object of wx.MenuItem class is used to append.

wx.Menu.Append(id, text, kind)
  
Item = Wx.MenuItem(parentmenu, id, text, kind) 
wx.Menu.Append(Item)

In order to define a menu item, the menu in which it is to be added must be mentioned.

wxPython has a large number of standard IDs to be assigned to standard menu items. On some OS platforms, they are associated with standard icons too.

wx.ID_SEPARATOR
wx.ID_ANY
wx.ID_OPEN
wx.ID_CLOSE
wx.ID_NEW
wx.ID_SAVE
wx.ID_SAVEAS
wx.ID_EDIT
wx.ID_CUT
wx.ID_COPY
wx.ID_PASTE

However, any unique integer number can be assigned as ID. The text parameter is its caption. Kind parameter takes one of the following enumerators −

S.N. Parameters & Description
1

wx.ITEM_NORMAL

Normal menu item

2

wx.ITEM_CHECK

Check (or toggle) menu item

3

wx.ITEM_RADIO

Radio menu item

wx.Menu class also has AppendRadioItem() and AppendCheckItem() that don’t require kind parameter.

A menu Item can be set to display an icon or shortcut. SetBitmap() function of wx.MenuItem class requires a bitmap object to be displayed.

wx.MenuItem.SetBitmap(wx.Bitmap(image file))

EVT_MENU event binder helps in processing the menu selection.

self.Bind(wx.EVT_MENU, self.menuhandler)

Example

The following example demonstrates most of the above mentioned features of menu system in wxPython. It shows a File menu displayed in the Menu bar. Normal menu item, a sub menu, radio items and check items are added into it. Menu items having an icon are also present.

Event handler, when invoked retrieves ID associated with the event and can be further processed. For instance, if ‘New’ menu item is selected, it is echoed in the text box on the frame.

The complete code is as follows −

import wx  

class Mywin(wx.Frame): 
            
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title, size = (250,150))  
      self.InitUI() 
         
   def InitUI(self):    
      menubar = wx.MenuBar() 
		
      fileMenu = wx.Menu() 
      newitem = wx.MenuItem(fileMenu,wx.ID_NEW, text = "New",kind = wx.ITEM_NORMAL) 
      newitem.SetBitmap(wx.Bitmap("new.bmp")) 
      fileMenu.AppendItem(newitem) 
		
      fileMenu.AppendSeparator()
		
      editMenu = wx.Menu() 
      copyItem = wx.MenuItem(editMenu, 100,text = "copy",kind = wx.ITEM_NORMAL)
      copyItem.SetBitmap(wx.Bitmap("copy.bmp")) 
		
      editMenu.AppendItem(copyItem) 
      cutItem = wx.MenuItem(editMenu, 101,text = "cut",kind = wx.ITEM_NORMAL) 
      cutItem.SetBitmap(wx.Bitmap("cut.bmp")) 
		
      editMenu.AppendItem(cutItem) 
      pasteItem = wx.MenuItem(editMenu, 102,text = "paste",kind = wx.ITEM_NORMAL) 
      pasteItem.SetBitmap(wx.Bitmap("paste.bmp")) 
		
      editMenu.AppendItem(pasteItem) 
      fileMenu.AppendMenu(wx.ID_ANY, "Edit", editMenu) 
      fileMenu.AppendSeparator() 
         
      radio1 = wx.MenuItem(fileMenu, 200,text = "Radio1",kind = wx.ITEM_RADIO) 
      radio2 = wx.MenuItem(fileMenu, 300,text = "radio2",kind = wx.ITEM_RADIO) 
		
      fileMenu.AppendItem(radio1) 
      fileMenu.AppendItem(radio2) 
      fileMenu.AppendSeparator() 
         
      fileMenu.AppendCheckItem(103,"Checkable") 
      quit = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+Q') 
		
      fileMenu.AppendItem(quit) 
      menubar.Append(fileMenu, '&File') 
		
      self.SetMenuBar(menubar) 
      self.text = wx.TextCtrl(self,-1, style = wx.EXPAND|wx.TE_MULTILINE) 
      self.Bind(wx.EVT_MENU, self.menuhandler) 
      self.SetSize((350, 250)) 
      self.Centre() 
      self.Show(True)
		
   def menuhandler(self, event): 
      id = event.GetId() 
      if id == wx.ID_NEW: 
         self.text.AppendText("new"+"\n")
			
ex = wx.App() 
Mywin(None,'MenuBar demo') 
ex.MainLoop() 

The above code produces the following output −

Menu Bar Output
wxpython_major_classes.htm
Advertisements