]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-10/popupmenu.py
Added the sample code from wxPython In Action to the samples dir
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-10 / popupmenu.py
1 import wx
2
3 class MyFrame(wx.Frame):
4 def __init__(self):
5 wx.Frame.__init__(self, None, -1,
6 "Popup Menu Example")
7 self.panel = p = wx.Panel(self)
8 menu = wx.Menu()
9 exit = menu.Append(-1, "Exit")
10 self.Bind(wx.EVT_MENU, self.OnExit, exit)
11
12 menuBar = wx.MenuBar()
13 menuBar.Append(menu, "Menu")
14 self.SetMenuBar(menuBar)
15
16 wx.StaticText(p, -1,
17 "Right-click on the panel to show a popup menu",
18 (25,25))
19
20 self.popupmenu = wx.Menu()
21 for text in "one two three four five".split():
22 item = self.popupmenu.Append(-1, text)
23 self.Bind(wx.EVT_MENU, self.OnPopupItemSelected, item)
24 p.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)
25
26
27 def OnShowPopup(self, event):
28 pos = event.GetPosition()
29 pos = self.panel.ScreenToClient(pos)
30 self.panel.PopupMenu(self.popupmenu, pos)
31
32
33 def OnPopupItemSelected(self, event):
34 item = self.popupmenu.FindItemById(event.GetId())
35 text = item.GetText()
36 wx.MessageBox("You selected item '%s'" % text)
37
38
39 def OnExit(self, event):
40 self.Close()
41
42
43 if __name__ == "__main__":
44 app = wx.PySimpleApp()
45 frame = MyFrame()
46 frame.Show()
47 app.MainLoop()
48
49