How do I create a menu and attach it to the menu bar

A wxPython menu bar consists of individual menus, each of which needs to be created separately. The following displays the constructor for wx.Menu.

There is only one valid style for wx.Menu instances. Under GTK, the style wx.MENU_ tearoff allows the menu to be detached from the menu bar and used as a standalone selector. Under other platforms, the style has no effect. If the platform supports it, a title can be given to the menu when it is created, which will add the text at the top of the menu above any regular menu items that are added to the menu. Figure 10.1 displays a blank window with three menus.

Listing 10.1 displays the series of menus being added to a menu bar, without any items being added.

Listing 10.1 Adding menus to a menu bar import wx class MyFrame(wx.Frame):

wx.Frame._init_(self, None, -1, "Simple Menu Example")

Figure 10.1

A blank window with three menus

Figure 10.1

A blank window with three menus menuBar = wx.MenuBar() <— Creating a menu bar menu = wx.Menu() <— Creating a menu menuBar.Append(menu, "Left Menu") menu2 = wx.Menu()

menuBar.Append(menu2, "Middle Menu") menu3 = wx.Menu()

menuBar.Append(menu3, "Right Menu") self.SetMenuBar(menuBar)

Appending the menu to the bar if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()

In the wxPython menu API, most of the manipulation of an object is managed by its container class. Later in this chapter, we'll discuss the specific methods of wx.Menu, since most of the methods concern manipulation of the menu items within the menu. In the remainder of this section, since we are talking about manipulating wx.Menu objects, we'll be listing the methods of wx.MenuBar that concern menus. Table 10.1 displays the four methods within wx.MenuBar that manipulate the contents of the menu bar.

Table 10.1 Methods of wx.MenuBar for manipulating the menus in the menu bar



Append(menu, title)

Appends the menu parameter to the end of the menu bar (the rightmost element displayed). The title parameter will be used to display the new menu. Returns True if successful, otherwise False.

Insert(pos, menu, title)

Inserts the given menu so that it is at the given pos (after this call, GetMenu(pos) == menu is true). As if inserting into a list, all the following menus are shifted to the right. The menu indexes are zero-based, so a pos of 0 is equivalent to putting the menu at the left of the menu bar. Inserting at GetMenuCount() as the pos is the same as using Append. The title is used for the display name. Returns True if successful.


Removes the menu at the position pos, shifting all other menus leftward. Returns the menu being removed.

Replace(pos, menu, title)

Replaces the menu at position pos, with the new menu passed in the menu parameter, and using the display name given by the title parameter. The other menus on the menu bar are unaffected. Returns the menu which was previously at the position pos.

The wx.MenuBar class contains a few other methods that manipulate the component menus in other ways, as displayed in table 10.2.

Table 10.2 Menu property methods of wx.MenuBar



EnableTop(pos, enable)

Sets the enable/disable state of the menu at position pos. If enable is True, then the menu is enabled, if False, then it is disabled.


Returns the menu object at the given position.


Returns the number of menus in the menu bar.


Returns the integer index of the menu in the menu bar with the given title. If there is no such menu, the method returns the constant wx.NOT_FOUND. The method will ignore decorations for keyboard shotcuts, if any.

GetLabelTop(pos) SetLabelTop(pos, label)

Getter and setter method for the display label of the menu at the given position.

Was this article helpful?

0 0

Post a comment