
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Popup Menu in wxPython
In this article we will get to know about wxPython and we will create a program to make a pop-up item menu.
Pop-up menus are graphical user interfaces (GUIs) which allow us to show some specific items and options. Here in this tutorial, we will learn how to create and implement popup menus in wxPython. It is a GUI toolkit for python language which provides us with a set of bindings and allows developers to create cross platform applications having native interface.
Installing wxPython Using pip Command
pip install wxPython
in case it is not getting installed type execute following command.
pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04/ wxPython
To display the message box, we will use a messagebox from easygui.
To install the easygui execute the following command.
pip install easygui
EasyGUI
it is also a graphical user interface which provides us a set of pre-built dialogs, and widgets which can be easily used without coding its core functional part like Tkinter or wxPython. Using EasyGUI we can create many more fields like message box, text field, buttn and other components.
Example
import wx import easygui class PopUpMenu(wx.Menu): def __init__(self): super().__init__() # Add a menu item self.item1 = self.Append(wx.ID_ANY, "Mango") # Bind an event handler to the mango menu item self.Bind(wx.EVT_MENU, self.mango, self.item1) # Add another menu item self.item2 = self.Append(wx.ID_ANY, "Litchi") # Bind an event handler to the litchi menu item self.Bind(wx.EVT_MENU, self.litchi, self.item2) # Add another menu item self.item3 = self.Append(wx.ID_ANY, "Apple") # Bind an event handler to the apple menu item self.Bind(wx.EVT_MENU, self.apple, self.item3) def mango(self, event): easygui.msgbox("You choose Mango!", title="msg") def litchi(self, event): easygui.msgbox("You choose Litchi!", title="msg") def apple(self, event): easygui.msgbox("You choose Apple!", title="msg") class FrameClass(wx.Frame): def __init__(self): super().__init__(None, title="Frame Simple") # Create a menu bar self.menubar = wx.MenuBar() # Add the pop-up menu to the menu bar self.menubar.Append(PopUpMenu(), "Fruit Menu") # Set the menu bar as the frame's menu bar self.SetMenuBar(self.menubar) # Bind a mouse event handler to the frame self.Bind(wx.EVT_RIGHT_DOWN, self.on_right_click) def on_right_click(self, event): self.PopupMenu(self.menubar, event.GetPosition()) if __name__ == "__main__": app = wx.App() # Creating a frame frame = FrameClass() # Show the frame frame.Show() app.MainLoop()
Output

Explanation
In the above program we create a frame using the menu bar. When you click on the frame a pop-up menu will be displayed. In the menu bar it will have a pop-up menu of fruit items Like "mango", "Litchi", "Apple". When you click or choose any of the items listed in the menu bar it will show you an action as a message containing the fruit item name selected.
Here in the PopUpMenu class we are creating a pop-up menu. In the class it contains a constructor which is used to create a pop-up menu having no menu items. In the class it has different methods named on fruit names. suppose you clicked on items named "Mango" then it will invoke the method named "mango" and this method will print the pop-up message that "You choose mango". Similarly, selection of other menu items will perform the same action by calling their own function.
So, we got to know about how we can create pop-up menu items in wxPython and how we can implement actions after selection of any specified menu item.