]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PopupMenu.py
2 from wxPython
.wx
import *
5 #----------------------------------------------------------------------
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.
17 #----------------------------------------------------------------------
19 class TestPanel(wxPanel
):
20 def __init__(self
, parent
, log
):
22 wxPanel
.__init
__(self
, parent
, -1)
23 box
= wxBoxSizer(wxVERTICAL
)
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
)
30 t
= wxStaticText(self
, -1, "PopupMenu")
32 box
.Add(t
, 0, wxCENTER|wxALL
, 5)
34 box
.Add(wxStaticLine(self
, -1), 0, wxEXPAND
)
37 t
= wxStaticText(self
, -1, text
)
39 box
.Add(t
, 0, wxCENTER|wxALL
, 5)
43 EVT_RIGHT_UP(self
, self
.OnRightClick
)
46 def OnRightClick(self
, event
):
47 self
.log
.WriteText("OnRightClick\n")
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
)
72 # Show how to put an icon in the menu
73 item
= wxMenuItem(menu
, self
.popupID1
,"One")
74 item
.SetBitmap(images
.getSmilesBitmap())
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")
84 sm
.Append(self
.popupID8
, "sub item 1")
85 sm
.Append(self
.popupID9
, "sub item 1")
86 menu
.AppendMenu(self
.popupID7
, "Test Submenu", sm
)
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())
95 def OnPopupOne(self
, event
):
96 self
.log
.WriteText("Popup one\n")
98 def OnPopupTwo(self
, event
):
99 self
.log
.WriteText("Popup two\n")
101 def OnPopupThree(self
, event
):
102 self
.log
.WriteText("Popup three\n")
104 def OnPopupFour(self
, event
):
105 self
.log
.WriteText("Popup four\n")
107 def OnPopupFive(self
, event
):
108 self
.log
.WriteText("Popup five\n")
110 def OnPopupSix(self
, event
):
111 self
.log
.WriteText("Popup six\n")
113 def OnPopupSeven(self
, event
):
114 self
.log
.WriteText("Popup seven\n")
116 def OnPopupEIght(self
, event
):
117 self
.log
.WriteText("Popup eight\n")
119 def OnPopupNine(self
, event
):
120 self
.log
.WriteText("Popup nine\n")
126 #----------------------------------------------------------------------
128 def runTest(frame
, nb
, log
):
129 win
= TestPanel(nb
, log
)
132 #----------------------------------------------------------------------
136 overview
= """<html><body>
137 <h2><center>PopupMenu</center></h2>
144 if __name__
== '__main__':
147 run
.main(['', os
.path
.basename(sys
.argv
[0])])