wxPython Tutorial – Create Menu

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

 

 

 

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.

 

 

 

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.

 

 

For runing the application, we need to create an instance of our main frame class and start the wxPython event loop.

 

 

 

This is the complete code 

 

 

 

 

 

Run the complete code and this will be the result

wxPython Tutorial - Create Menu
wxPython Tutorial – Create Menu

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×