Open In App

Python – popup menu in wxPython

Last Updated : 24 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we are going to know how can we create a popupmenu in wxPython. We will write a code when we click right on screen a popup menu will show up with menu items names as ‘one’ and ‘two’.

Syntax :

wx.Window.PopupMenu(self, menu, pos)

Parameters :

Parameter Input Type Description
menu wx.Menu Menu in popupmenu.
point wx.Point point of popup menu.

Code Example : 

Python3




import wx
 
class PopMenu(wx.Menu):
 
    def __init__(self, parent):
        super(PopMenu, self).__init__()
 
        self.parent = parent
 
        # menu item 1
        popmenu = wx.MenuItem(self, wx.NewId(), 'one ')
        self.Append(popmenu)
        # menu item 2
        popmenu2 = wx.MenuItem(self, wx.NewId(), 'two')
        self.Append(popmenu2)
 
class Example(wx.Frame):
 
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
 
        self.InitUI()
 
    def InitUI(self):
 
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
 
        self.SetSize((600, 400))
        self.SetTitle('Popup Menu')
        self.Centre()
 
    def OnRightDown(self, e):
        # show popup menu
        self.PopupMenu(PopMenu(self), e.GetPosition())
 
 
def main():
 
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
 
 
if __name__ == '__main__':
    main()


Output :



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads