| 1 | |
| 2 | from wxPython.wx import * |
| 3 | import images |
| 4 | |
| 5 | #---------------------------------------------------------------------- |
| 6 | |
| 7 | text = """\ |
| 8 | |
| 9 | Right-click on the panel (or Ctrl-click on the Mac) to show a popup |
| 10 | menu. Then look at the code for this sample. Notice how the |
| 11 | PopupMenu method is similar to the ShowModal method of a wxDialog in |
| 12 | that it doesn't return until the popup menu has been dismissed. The |
| 13 | event handlers for the popup menu items can either be attached to the |
| 14 | menu itself, or to the window that invokes PopupMenu. |
| 15 | """ |
| 16 | |
| 17 | #---------------------------------------------------------------------- |
| 18 | |
| 19 | class TestPanel(wxPanel): |
| 20 | def __init__(self, parent, log): |
| 21 | self.log = log |
| 22 | wxPanel.__init__(self, parent, -1) |
| 23 | box = wxBoxSizer(wxVERTICAL) |
| 24 | |
| 25 | # Make and layout the controls |
| 26 | fs = self.GetFont().GetPointSize() |
| 27 | bf = wxFont(fs+4, wxSWISS, wxNORMAL, wxBOLD) |
| 28 | nf = wxFont(fs+2, wxSWISS, wxNORMAL, wxNORMAL) |
| 29 | |
| 30 | t = wxStaticText(self, -1, "PopupMenu") |
| 31 | t.SetFont(bf) |
| 32 | box.Add(t, 0, wxCENTER|wxALL, 5) |
| 33 | |
| 34 | box.Add(wxStaticLine(self, -1), 0, wxEXPAND) |
| 35 | box.Add(10,20) |
| 36 | |
| 37 | t = wxStaticText(self, -1, text) |
| 38 | t.SetFont(nf) |
| 39 | box.Add(t, 0, wxCENTER|wxALL, 5) |
| 40 | |
| 41 | self.SetSizer(box) |
| 42 | |
| 43 | EVT_RIGHT_UP(self, self.OnRightClick) |
| 44 | |
| 45 | |
| 46 | def OnRightClick(self, event): |
| 47 | self.log.WriteText("OnRightClick\n") |
| 48 | |
| 49 | # only do this part the first time so the events are only bound once |
| 50 | if not hasattr(self, "popupID1"): |
| 51 | self.popupID1 = wxNewId() |
| 52 | self.popupID2 = wxNewId() |
| 53 | self.popupID3 = wxNewId() |
| 54 | self.popupID4 = wxNewId() |
| 55 | self.popupID5 = wxNewId() |
| 56 | self.popupID6 = wxNewId() |
| 57 | self.popupID7 = wxNewId() |
| 58 | self.popupID8 = wxNewId() |
| 59 | self.popupID9 = wxNewId() |
| 60 | EVT_MENU(self, self.popupID1, self.OnPopupOne) |
| 61 | EVT_MENU(self, self.popupID2, self.OnPopupTwo) |
| 62 | EVT_MENU(self, self.popupID3, self.OnPopupThree) |
| 63 | EVT_MENU(self, self.popupID4, self.OnPopupFour) |
| 64 | EVT_MENU(self, self.popupID5, self.OnPopupFive) |
| 65 | EVT_MENU(self, self.popupID6, self.OnPopupSix) |
| 66 | EVT_MENU(self, self.popupID7, self.OnPopupSeven) |
| 67 | EVT_MENU(self, self.popupID8, self.OnPopupEIght) |
| 68 | EVT_MENU(self, self.popupID9, self.OnPopupNine) |
| 69 | |
| 70 | # make a menu |
| 71 | menu = wxMenu() |
| 72 | # Show how to put an icon in the menu |
| 73 | item = wxMenuItem(menu, self.popupID1,"One") |
| 74 | item.SetBitmap(images.getSmilesBitmap()) |
| 75 | menu.AppendItem(item) |
| 76 | # add some other items |
| 77 | menu.Append(self.popupID2, "Two") |
| 78 | menu.Append(self.popupID3, "Three") |
| 79 | menu.Append(self.popupID4, "Four") |
| 80 | menu.Append(self.popupID5, "Five") |
| 81 | menu.Append(self.popupID6, "Six") |
| 82 | # make a submenu |
| 83 | sm = wxMenu() |
| 84 | sm.Append(self.popupID8, "sub item 1") |
| 85 | sm.Append(self.popupID9, "sub item 1") |
| 86 | menu.AppendMenu(self.popupID7, "Test Submenu", sm) |
| 87 | |
| 88 | |
| 89 | # Popup the menu. If an item is selected then its handler |
| 90 | # will be called before PopupMenu returns. |
| 91 | self.PopupMenu(menu, event.GetPosition()) |
| 92 | menu.Destroy() |
| 93 | |
| 94 | |
| 95 | def OnPopupOne(self, event): |
| 96 | self.log.WriteText("Popup one\n") |
| 97 | |
| 98 | def OnPopupTwo(self, event): |
| 99 | self.log.WriteText("Popup two\n") |
| 100 | |
| 101 | def OnPopupThree(self, event): |
| 102 | self.log.WriteText("Popup three\n") |
| 103 | |
| 104 | def OnPopupFour(self, event): |
| 105 | self.log.WriteText("Popup four\n") |
| 106 | |
| 107 | def OnPopupFive(self, event): |
| 108 | self.log.WriteText("Popup five\n") |
| 109 | |
| 110 | def OnPopupSix(self, event): |
| 111 | self.log.WriteText("Popup six\n") |
| 112 | |
| 113 | def OnPopupSeven(self, event): |
| 114 | self.log.WriteText("Popup seven\n") |
| 115 | |
| 116 | def OnPopupEIght(self, event): |
| 117 | self.log.WriteText("Popup eight\n") |
| 118 | |
| 119 | def OnPopupNine(self, event): |
| 120 | self.log.WriteText("Popup nine\n") |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | #---------------------------------------------------------------------- |
| 127 | |
| 128 | def runTest(frame, nb, log): |
| 129 | win = TestPanel(nb, log) |
| 130 | return win |
| 131 | |
| 132 | #---------------------------------------------------------------------- |
| 133 | |
| 134 | |
| 135 | |
| 136 | overview = """<html><body> |
| 137 | <h2><center>PopupMenu</center></h2> |
| 138 | """ + text + """ |
| 139 | </body></html> |
| 140 | """ |
| 141 | |
| 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | import sys,os |
| 146 | import run |
| 147 | run.main(['', os.path.basename(sys.argv[0])]) |
| 148 | |