python logo

wxpython menu


Python hosting: Host, run, and code Python in the cloud!

Most desktop applications feature a window menu, which can vary in appearance based on the operating system in use.

With wxPython, desktop applications seamlessly integrate to appear as native applications on any operating system. If you’re aiming for a consistent appearance across all platforms, it might be more suitable to explore a different GUI framework.

Menu in a wxPython application

Recommended course: Creating GUI Applications with wxPython

wxPython Menu Creation

The following code demonstrates how to create a menubar in a wxPython window:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python

import wx

app = wx.App()

frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,50)

# Initialize the menu.
filemenu= wx.Menu()
filemenu.Append(101, "Open", "Open")
filemenu.Append(102, "Save", "Save")
filemenu.Append(wx.ID_ABOUT, "About","About")
filemenu.Append(wx.ID_EXIT,"Exit","Close")

# Construct the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"File") # Integrating the "filemenu" to the MenuBar
frame.SetMenuBar(menuBar) # Attaching the MenuBar to the Frame.
frame.Show()

app.MainLoop()

Creating a menu in wxPython starts with initializing a wx.MenuBar().

A standalone menu isn’t sufficient, as it requires several sub-menus for effective functionality, like a File menu. These sub-menus are generated using wx.Menu(), each housing multiple items.

After designing the desired sub-menus, bind the frame’s menubar to the one crafted.

wxPython also provides default ids such as wx.ID_ABOUT and wx.ID_EXIT. These are essentially integer values. However, for customization, you can designate your own ids, like the example showcases with 101 and 102.

Previous Tutorial | Next Tutorial






Leave a Reply: