| 1 | import wx |
| 2 | print wx.VERSION |
| 3 | |
| 4 | ID_ITEM1 = wx.NewId() |
| 5 | ID_ITEM2 = wx.NewId() |
| 6 | |
| 7 | |
| 8 | |
| 9 | class TestPanelBase(wx.Panel): |
| 10 | def __init__(self, parent): |
| 11 | wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN) |
| 12 | self.item1 = False |
| 13 | self.item2 = True |
| 14 | self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu) |
| 15 | self.Bind(wx.EVT_MENU, self.OnItem1, id=ID_ITEM1) |
| 16 | self.Bind(wx.EVT_MENU, self.OnItem2, id=ID_ITEM2) |
| 17 | wx.StaticText(self, -1, self.__doc__.strip(), (0,0)) |
| 18 | |
| 19 | def OnItem1(self, evt): |
| 20 | print "OnItem1" |
| 21 | self.item1 = not self.item1 |
| 22 | |
| 23 | def OnItem2(self, evt): |
| 24 | print "OnItem2" |
| 25 | self.item2 = not self.item2 |
| 26 | |
| 27 | |
| 28 | class TestPanel1(TestPanelBase): |
| 29 | """ |
| 30 | Pre-create and reuse the menu |
| 31 | """ |
| 32 | def __init__(self, parent): |
| 33 | TestPanelBase.__init__(self, parent) |
| 34 | self.menu = wx.Menu() |
| 35 | self.menu.Append(ID_ITEM1, "Item 1", kind=wx.ITEM_CHECK) |
| 36 | self.menu.Append(ID_ITEM2, "Item 2", kind=wx.ITEM_CHECK) |
| 37 | self.menu.Check(ID_ITEM1, self.item1) |
| 38 | self.menu.Check(ID_ITEM2, self.item2) |
| 39 | |
| 40 | def OnContextMenu(self, evt): |
| 41 | self.PopupMenu(self.menu) |
| 42 | |
| 43 | |
| 44 | class TestPanel2(TestPanelBase): |
| 45 | """ |
| 46 | This one recreates the menu and sets the checks each time |
| 47 | """ |
| 48 | def __init__(self, parent): |
| 49 | TestPanelBase.__init__(self, parent) |
| 50 | |
| 51 | def OnContextMenu(self, evt): |
| 52 | menu = wx.Menu() |
| 53 | menu.Append(ID_ITEM1, "Item 1", kind=wx.ITEM_CHECK) |
| 54 | menu.Append(ID_ITEM2, "Item 2", kind=wx.ITEM_CHECK) |
| 55 | menu.Check(ID_ITEM1, self.item1) |
| 56 | menu.Check(ID_ITEM2, self.item2) |
| 57 | self.PopupMenu(menu) |
| 58 | menu.Destroy() |
| 59 | |
| 60 | |
| 61 | class TestPanel3(TestPanelBase): |
| 62 | """ |
| 63 | Use an EVT_UPDATE_UI handler to set the checks |
| 64 | """ |
| 65 | def __init__(self, parent): |
| 66 | TestPanelBase.__init__(self, parent) |
| 67 | self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUI_Item1, id=ID_ITEM1) |
| 68 | self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUI_Item2, id=ID_ITEM2) |
| 69 | |
| 70 | def OnContextMenu(self, evt): |
| 71 | menu = wx.Menu() |
| 72 | menu.Append(ID_ITEM1, "Item 1", kind=wx.ITEM_CHECK) |
| 73 | menu.Append(ID_ITEM2, "Item 2", kind=wx.ITEM_CHECK) |
| 74 | self.PopupMenu(menu) |
| 75 | menu.Destroy() |
| 76 | |
| 77 | def OnUpdateUI_Item1(self, evt): |
| 78 | print "OnUpdateUI_Item1" |
| 79 | evt.Check(self.item1) |
| 80 | |
| 81 | def OnUpdateUI_Item2(self, evt): |
| 82 | print "OnUpdateUI_Item2" |
| 83 | evt.Check(self.item2) |
| 84 | |
| 85 | |
| 86 | app = wx.App(False) |
| 87 | frm = wx.Frame(None, title="Right-click on a panel...") |
| 88 | pnl1 = TestPanel1(frm) |
| 89 | pnl2 = TestPanel2(frm) |
| 90 | pnl3 = TestPanel3(frm) |
| 91 | sizer = wx.BoxSizer(wx.VERTICAL) |
| 92 | frm.SetSizer(sizer) |
| 93 | sizer.Add(pnl1, 1, wx.EXPAND) |
| 94 | sizer.Add(pnl2, 1, wx.EXPAND) |
| 95 | sizer.Add(pnl3, 1, wx.EXPAND) |
| 96 | frm.Show() |
| 97 | app.MainLoop() |