article

Friday, May 27, 2016

wxpython Context menu right click example

wxpython Context menu right click example
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
46
47
48
49
50
51
52
53
import wx
 
class MyPopupMenu(wx.Menu):
     
    def __init__(self, parent):
        super(MyPopupMenu, self).__init__()
         
        self.parent = parent
 
        mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
        self.AppendItem(mmi)
        self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
 
        cmi = wx.MenuItem(self, wx.NewId(), 'Close')
        self.AppendItem(cmi)
        self.Bind(wx.EVT_MENU, self.OnClose, cmi)
 
 
    def OnMinimize(self, e):
        self.parent.Iconize()
 
    def OnClose(self, e):
        self.parent.Close()
         
 
class Example(wx.Frame):
     
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
             
        self.InitUI()
         
    def InitUI(self):
        #If right click on the frame, call the OnRightDown() method
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
 
        self.SetSize((250, 200))
        self.SetTitle('Context menu')
        self.Centre()
        self.Show(True)
         
    def OnRightDown(self, e):
        self.PopupMenu(MyPopupMenu(self), e.GetPosition()) #call MyPopupMenu class
        # PopupMenu() method  shows the context menu, GetPosition() The context menus appear at the point of the mouse cursor
def main():
     
    ex = wx.App()
    Example(None)
    ex.MainLoop()   
 
 
if __name__ == '__main__':
    main()

Related Post