In this wxPython Tutorial we want to learn that How to Create Menu in wxPython, creating a menu is easy, you can simply use 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, 'Codeloop.org - Menu Example') app.MainLoop() |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import wx class MyApp(wx.Frame): def __init__(self, parent, title): # Call the __init__ method super(MyApp, self).__init__(parent, title=title, size=(400, 300)) # Set icon for the frame self.SetIcon(wx.Icon("codeloop.png")) # Call the method to initialize the user interface self.InitUI() def InitUI(self): # Create a menu bar menubar = wx.MenuBar() # Create a File menu fileMenu = wx.Menu() # Add a Quit item to the File menu quitMenuItem = fileMenu.Append(wx.ID_EXIT, "Quit", "Quit the application") # Add the File menu to the menu bar menubar.Append(fileMenu, "&File") # Set the menu bar for the frame self.SetMenuBar(menubar) # Bind the Quit menu item to the OnQuit method self.Bind(wx.EVT_MENU, self.OnQuit, quitMenuItem) # Show the frame self.Show(True) # Define the method to be called def OnQuit(self, e): # Close the frame self.Close() if __name__ == '__main__': # Create a new application object app = wx.App() # Create an instance of MyApp MyApp(None, 'Codeloop.org - Menu Example') # Enter the application's main loop app.MainLoop() |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email