In this wxPython article we want to learn about How to Create Menu in wxPython with wx.Menu, creating a menu is a straightforward process that involves using wx.Menu and wx.MenuBar classes. Menus provides an easy way for users to access application functionality and features.
First we need to import our required modules from wxPython
1 2 |
import wx import sys |
After that we need to create the main frame of our application. this will serve as the container for the menu and other GUI components, in this code we have created the main frame of our application by inheriting from wx.Frame class. we set the title of the frame and its size. after that we define InitUI() method, in this method we creates the menu bar using wx.MenuBar class. we also creates a File menu using wx.Menu class and add a Quit item to the menu using Append method. also we add the File menu to the menu bar using Append method, and set the menu bar using SetMenuBar method. and lastly we bind the OnQuit() method to the quitMenuItem using Bind method, and we show the frame using the Show method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class MyApp(wx.Frame): def __init__(self, parent, title): super(MyApp, self).__init__(parent, title=title, size=(400, 300)) self.InitUI() def InitUI(self): menubar = wx.MenuBar() fileMenu = wx.Menu() quitMenuItem = fileMenu.Append(wx.ID_EXIT, "Quit", "Quit the application") menubar.Append(fileMenu, "&File") self.SetMenuBar(menubar) self.Bind(wx.EVT_MENU, self.OnQuit, quitMenuItem) self.Show(True) def OnQuit(self, e): self.Close() |
OnQuit() method is responsible for handling the Quit menu item event. when the user clicks on the Quit menu item, we want to exit the application. we do this by calling Close() method of the frame.
1 2 |
def OnQuit(self, e): self.Close() |
For runing the application, we need to create an instance of our main frame class and start the wxPython event loop.
1 2 3 4 |
if __name__ == '__main__': app = wx.App() MyApp(None, 'Menu Example') app.MainLoop() |
This is the complete code for this article

Learn More on wxPython
- How to Create wxPython Main Window wx.Frame
- How to Create wxPython Panel with wx.Panel
- How to Create wxPython Button
- How to Create wxPython Text Control
- How to Create wxPython Static Text
- How to Create wxPython CheckBox
- How to Create wxPython RadioButton
- How to Create List Box in wxPython
- How to Create ComboBox in wxPython
- How to Create Slider in wxPython