Popup Menu in wxPython

In this article, we will explore wxPython and create a popup menu application. Popup menus are contextual GUI elements that appear when triggered, typically by right-clicking, providing users with relevant options and actions.

wxPython is a powerful GUI toolkit for Python that provides native-looking applications across different platforms. We'll learn how to create and implement popup menus with interactive menu items.

Installing Required Libraries

First, install wxPython using pip ?

pip install wxPython

If the installation fails, try this alternative command for Linux ?

pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04/ wxPython

For displaying message dialogs, we'll use EasyGUI ?

pip install easygui

What is EasyGUI?

EasyGUI is a simple GUI library that provides pre-built dialogs and widgets. It simplifies creating message boxes, input fields, and buttons without complex setup, making it perfect for quick dialog implementations.

Creating a Popup Menu

Here's a complete example that creates a fruit selection popup menu ?

import wx
import easygui

class PopUpMenu(wx.Menu):
    def __init__(self):
        super().__init__()

        # Add menu items
        self.item1 = self.Append(wx.ID_ANY, "Mango")
        self.item2 = self.Append(wx.ID_ANY, "Litchi")  
        self.item3 = self.Append(wx.ID_ANY, "Apple")

        # Bind event handlers
        self.Bind(wx.EVT_MENU, self.mango, self.item1)
        self.Bind(wx.EVT_MENU, self.litchi, self.item2)
        self.Bind(wx.EVT_MENU, self.apple, self.item3)

    def mango(self, event):
        easygui.msgbox("You chose Mango!", title="Selection")

    def litchi(self, event):
        easygui.msgbox("You chose Litchi!", title="Selection")

    def apple(self, event):
        easygui.msgbox("You chose Apple!", title="Selection")


class FrameClass(wx.Frame):
    def __init__(self):
        super().__init__(None, title="Popup Menu Example")
        
        # Create and set menu bar
        self.menubar = wx.MenuBar()
        self.menubar.Append(PopUpMenu(), "Fruit Menu")
        self.SetMenuBar(self.menubar)

        # Bind right-click event for popup
        self.Bind(wx.EVT_RIGHT_DOWN, self.on_right_click)
        
        # Add instructions
        panel = wx.Panel(self)
        text = wx.StaticText(panel, label="Right-click anywhere to show popup menu")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text, 0, wx.ALL | wx.CENTER, 20)
        panel.SetSizer(sizer)

    def on_right_click(self, event):
        # Create popup menu instance
        popup_menu = PopUpMenu()
        self.PopupMenu(popup_menu, event.GetPosition())


if __name__ == "__main__":
    app = wx.App()
    frame = FrameClass()
    frame.Show()
    app.MainLoop()

How It Works

The application consists of two main classes ?

  • PopUpMenu: Inherits from wx.Menu and defines menu items with their event handlers
  • FrameClass: Main window that handles right-click events and displays the popup menu

When you right-click anywhere in the window, the on_right_click method creates a popup menu at the cursor position. Selecting any fruit item triggers the corresponding method, displaying a message dialog.

Key Components

Component Purpose Method
Menu Creation Add menu items Append()
Event Binding Connect actions to items Bind()
Popup Display Show context menu PopupMenu()

Conclusion

Popup menus in wxPython provide an intuitive way to offer contextual options to users. By combining menu creation, event handling, and popup display, you can create interactive GUI applications with responsive user interfaces.

Updated on: 2026-03-27T15:10:19+05:30

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements