File: Menu.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (322 lines) | stat: -rw-r--r-- 11,117 bytes parent folder | download | duplicates (2)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python

import time
import wx
import images

#-------------------------------------------------------------------

class MyFrame(wx.Frame):

    def __init__(self, parent, id, log):
        wx.Frame.__init__(self, parent, id, 'Playing with menus', size=(500, 250))
        self.log = log
        self.CenterOnScreen()

        self.CreateStatusBar()
        self.SetStatusText("This is the statusbar")

        tc = wx.TextCtrl(self, -1, """
A bunch of bogus menus have been created for this frame.  You
can play around with them to see how they behave and then
check the source for this sample to see how to implement them.
""", style=wx.TE_READONLY|wx.TE_MULTILINE)

        # Prepare the menu bar
        menuBar = wx.MenuBar()

        # 1st menu from left
        menu1 = wx.Menu()
        menu1.Append(101, "&Mercury", "This the text in the Statusbar")
        menu1.Append(102, "&Venus", "")
        if False:
            # This is how you would create the menu item if you want to
            # change some of the visible attributes.
            item = wx.MenuItem(id=103, text="&Earth", helpString="You may select Earth too")
            item.SetFont(wx.Font(wx.FontInfo(10).Bold()))
            menu1.Append(item)
        else:
            # But we'll just do it the normal way for this sample
            menu1.Append(103, "&Earth", "You may select Earth too")
        menu1.AppendSeparator()
        menu1.Append(104, "&Close", "Close this frame")
        # Add menu to the menu bar
        menuBar.Append(menu1, "&Planets")

        # 2nd menu from left
        menu2 = wx.Menu()
        menu2.Append(201, "Hydrogen")
        menu2.Append(202, "Helium")
        # a submenu in the 2nd menu
        submenu = wx.Menu()
        submenu.Append(2031,"Lanthanium")
        submenu.Append(2032,"Cerium")
        submenu.Append(2033,"Praseodymium")
        menu2.Append(203, "Lanthanides", submenu)
        # Append 2nd menu
        menuBar.Append(menu2, "&Elements")

        menu3 = wx.Menu()
        # Radio items
        menu3.Append(301, "IDLE", "a Python shell using tcl/tk as GUI", wx.ITEM_RADIO)
        menu3.Append(302, "PyCrust", "a Python shell using wxPython as GUI", wx.ITEM_RADIO)
        menu3.Append(303, "psi", "a simple Python shell using wxPython as GUI", wx.ITEM_RADIO)
        menu3.AppendSeparator()
        menu3.Append(304, "project1", "", wx.ITEM_NORMAL)
        menu3.Append(305, "project2", "", wx.ITEM_NORMAL)
        menuBar.Append(menu3, "&Shells")

        menu4 = wx.Menu()
        # Check menu items
        menu4.Append(401, "letters", "abcde...", wx.ITEM_CHECK)
        menu4.Append(402, "digits", "123...", wx.ITEM_CHECK)
        menu4.Append(403, "letters and digits", "abcd... + 123...", wx.ITEM_CHECK)
        menuBar.Append(menu4, "Chec&k")

        menu5 = wx.Menu()
        # Show how to put an icon in the menu item
        item = wx.MenuItem(menu5, 500, "&Smile!\tCtrl+S", "This one has an icon")
        item.SetBitmap(images.Smiles.GetBitmap())
        menu5.Append(item)

        menuitemwithbmp = wx.MenuItem(menu5, wx.ID_ANY, "Submenu with Bitmap")
        # Show how to change the background colour of the menu item
        menuitemwithbmp.SetBackgroundColour(wx.YELLOW)
        # Show how to change the menu item's text colour
        menuitemwithbmp.SetTextColour(wx.BLUE)
        # Show how to change the menu item's font
        menuitemwithbmp.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, ''))
        submenu = wx.Menu(style=wx.MENU_TEAROFF)
        submenu.Append(wx.MenuItem(menu5, wx.ID_ANY, "Woot!"))
        menuitemwithbmp.SetBitmap(images.book.GetBitmap())
        menuitemwithbmp.SetSubMenu(submenu)
        menu5.Append(menuitemwithbmp)

        # Shortcuts
        menu5.Append(501, "Interesting thing\tCtrl+A", "Note the shortcut!")
        menu5.AppendSeparator()
        menu5.Append(502, "Hello\tShift+H")
        menu5.AppendSeparator()
        menu5.Append(503, "remove the submenu")
        menu6 = wx.Menu()
        menu6.Append(601, "Submenu Item")
        menu5.Append(504, "submenu", menu6)
        menu5.Append(505, "remove this menu")
        menu5.Append(506, "this is updated")
        menu5.Append(507, "insert after this...")
        menu5.Append(508, "...and before this")
        menuBar.Append(menu5, "&Fun")

        self.SetMenuBar(menuBar)

        # Menu events
        self.Bind(wx.EVT_MENU_HIGHLIGHT_ALL, self.OnMenuHighlight)

        self.Bind(wx.EVT_MENU, self.Menu101, id=101)
        self.Bind(wx.EVT_MENU, self.Menu102, id=102)
        self.Bind(wx.EVT_MENU, self.Menu103, id=103)
        self.Bind(wx.EVT_MENU, self.CloseWindow, id=104)

        self.Bind(wx.EVT_MENU, self.Menu201, id=201)
        self.Bind(wx.EVT_MENU, self.Menu202, id=202)
        self.Bind(wx.EVT_MENU, self.Menu2031, id=2031)
        self.Bind(wx.EVT_MENU, self.Menu2032, id=2032)
        self.Bind(wx.EVT_MENU, self.Menu2033, id=2033)

        self.Bind(wx.EVT_MENU, self.Menu301To303, id=301)
        self.Bind(wx.EVT_MENU, self.Menu301To303, id=302)
        self.Bind(wx.EVT_MENU, self.Menu301To303, id=303)
        self.Bind(wx.EVT_MENU, self.Menu304, id=304)
        self.Bind(wx.EVT_MENU, self.Menu305, id=305)

        # Range of menu items
        self.Bind(wx.EVT_MENU_RANGE, self.Menu401To403, id=401, id2=403)

        self.Bind(wx.EVT_MENU, self.Menu500, id=500)
        self.Bind(wx.EVT_MENU, self.Menu501, id=501)
        self.Bind(wx.EVT_MENU, self.Menu502, id=502)
        self.Bind(wx.EVT_MENU, self.TestRemove, id=503)
        self.Bind(wx.EVT_MENU, self.TestRemove2, id=505)
        self.Bind(wx.EVT_MENU, self.TestInsert, id=507)
        self.Bind(wx.EVT_MENU, self.TestInsert, id=508)

        wx.GetApp().Bind(wx.EVT_UPDATE_UI, self.TestUpdateUI, id=506)

    # Methods

    def OnMenuHighlight(self, event):
        # Show how to get menu item info from this event handler
        id = event.GetMenuId()
        item = self.GetMenuBar().FindItemById(id)
        if item:
            text = item.GetItemLabelText()
            help = item.GetHelp()

        # but in this case just call Skip so the default is done
        event.Skip()


    def Menu101(self, event):
        self.log.write('Welcome to Mercury\n')

    def Menu102(self, event):
        self.log.write('Welcome to Venus\n')

    def Menu103(self, event):
        self.log.write('Welcome to the Earth\n')

    def CloseWindow(self, event):
        self.Close()

    def Menu201(self, event):
        self.log.write('Chemical element number 1\n')

    def Menu202(self, event):
        self.log.write('Chemical element number 2\n')

    def Menu2031(self, event):
        self.log.write('Element number 57\n')

    def Menu2032(self, event):
        self.log.write('Element number 58\n')

    def Menu2033(self, event):
        self.log.write('Element number 59\n')

    def Menu301To303(self, event):
        id = event.GetId()
        self.log.write('Event id: %d\n' % id)

    def Menu304(self, event):
        self.log.write('Not yet available\n')

    def Menu305(self, event):
        self.log.write('Still vapour\n')

    def Menu401To403(self, event):
        self.log.write('From a EVT_MENU_RANGE event\n')

    def Menu500(self, event):
        self.log.write('Have a happy day!\n')

    def Menu501(self, event):
        self.log.write('Look in the code how the shortcut has been realized\n')

    def Menu502(self, event):
        self.log.write('Hello from Jean-Michel\n')


    def TestRemove(self, evt):
        mb = self.GetMenuBar()
        submenuItem = mb.FindItemById(601)

        if not submenuItem:
            return

        submenu = submenuItem.GetMenu()
        menu = submenu.GetParent()

        # This works
        #menu.Remove(504)

        # this also works
        menu.RemoveItem(mb.FindItemById(504))

        # This doesn't work, as expected since submenuItem is not on menu
        #menu.RemoveItem(submenuItem)


    def TestRemove2(self, evt):
        mb = self.GetMenuBar()
        mb.Remove(4)


    def TestUpdateUI(self, evt):
        text = time.ctime()
        evt.SetText(text)


    def TestInsert(self, evt):
        theID = 508
        # get the menu
        mb = self.GetMenuBar()
        menuItem = mb.FindItemById(theID)
        menu = menuItem.GetMenu()

        # figure out the position to insert at
        pos = 0

        for i in menu.GetMenuItems():
            if i.GetId() == theID:
                break

            pos += 1

        # now insert the new item
        ID = wx.NewIdRef()
        item = wx.MenuItem(menu, ID, "NewItem " + str(ID))
        menu.InsertItem(pos, item)


#---------------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        self.log = log
        wx.Panel.__init__(self, parent, -1)

        b = wx.Button(self, -1, "Show the Menu sample", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)


    def OnButton(self, evt):
        win = MyFrame(self, -1, self.log)
        win.Show(True)


#---------------------------------------------------------------------------


def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

#-------------------------------------------------------------------


overview = """\
A demo of using wx.MenuBar and wx.Menu in various ways.

A menu is a popup (or pull down) list of items, one of which may be selected
before the menu goes away (clicking elsewhere dismisses the menu). Menus may be
used to construct either menu bars or popup menus.

A menu item has an integer ID associated with it which can be used to identify
the selection, or to change the menu item in some way. A menu item with a special
identifier -1 is a separator item and doesn't have an associated command but just
makes a separator line appear in the menu.

Menu items may be either normal items, check items or radio items. Normal items
don't have any special properties while the check items have a boolean flag associated
to them and they show a checkmark in the menu when the flag is set. wxWindows
automatically toggles the flag value when the item is clicked and its value may
be retrieved using either IsChecked method of wx.Menu or wx.MenuBar itself or by
using wxEvent.IsChecked when you get the menu notification for the item in question.

The radio items are similar to the check items except that all the other items
in the same radio group are unchecked when a radio item is checked. The radio group
is formed by a contiguous range of radio items, i.e. it starts at the first item of
this kind and ends with the first item of a different kind (or the end of the menu).
Notice that because the radio groups are defined in terms of the item positions
inserting or removing the items in the menu containing the radio items risks to not
work correctly. Finally note that the radio items are only supported under Windows
and GTK+ currently.

"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])