Esto debería hacerlo. Veamos el programa de ejemplo menu.py para ayudarnos a clarificar los conceptos. La figura Figura 11.1. Ejemplo de Menú muestra la ventana del programa:
El código fuente de menu.py es:
1 #!/usr/bin/env python
2
3 # example menu.py
4
5 import gtk
6
7 class MenuExample:
8 def __init__(self):
9 # create a new window
10 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
11 window.set_size_request(200, 100)
12 window.set_title("GTK Menu Test")
13 window.connect("delete_event", gtk.mainquit)
14
15 # Init the menu-widget, and remember -- never
16 # show() the menu widget!!
17 # This is the menu that holds the menu items, the one that
18 # will pop up when you click on the "Root Menu" in the app
19 menu = gtk.Menu()
20
21 # Next we make a little loop that makes three menu-entries for
22 # "test-menu". Notice the call to gtk_menu_append. Here we are
23 # adding a list of menu items to our menu. Normally, we'd also
24 # catch the "clicked" signal on each of the menu items and setup a
25 # callback for it, but it's omitted here to save space.
26 for i in range(3):
27 # Copy the names to the buf.
28 buf = "Test-undermenu - %d" % i
29
30 # Create a new menu-item with a name...
31 menu_items = gtk.MenuItem(buf)
32
33 # ...and add it to the menu.
34 menu.append(menu_items)
35
36 # Do something interesting when the menuitem is selected
37 menu_items.connect("activate", self.menuitem_response, buf)
38
39 # Show the widget
40 menu_items.show()
41
42 # This is the root menu, and will be the label
43 # displayed on the menu bar. There won't be a signal handler attached,
44 # as it only pops up the rest of the menu when pressed.
45 root_menu = gtk.MenuItem("Root Menu")
46
47 root_menu.show()
48
49 # Now we specify that we want our newly created "menu" to be the
50 # menu for the "root menu"
51 root_menu.set_submenu(menu)
52
53 # A vbox to put a menu and a button in:
54 vbox = gtk.VBox(gtk.FALSE, 0)
55 window.add(vbox)
56 vbox.show()
57
58 # Create a menu-bar to hold the menus and add it to our main window
59 menu_bar = gtk.MenuBar()
60 vbox.pack_start(menu_bar, gtk.FALSE, gtk.FALSE, 2)
61 menu_bar.show()
62
63 # Create a button to which to attach menu as a popup
64 button = gtk.Button("press me")
65 button.connect_object("event", self.button_press, menu)
66 vbox.pack_end(button, gtk.TRUE, gtk.TRUE, 2)
67 button.show()
68
69 # And finally we append the menu-item to the menu-bar -- this is the
70 # "root" menu-item I have been raving about =)
71 menu_bar.append (root_menu)
72
73 # always display the window as the last step so it all splashes on
74 # the screen at once.
75 window.show()
76
77 # Respond to a button-press by posting a menu passed in as widget.
78 #
79 # Note that the "widget" argument is the menu being posted, NOT
80 # the button that was pressed.
81 def button_press(self, widget, event):
82 if event.type == gtk.gdk.BUTTON_PRESS:
83 widget.popup(None, None, None, event.button, event.time)
84 # Tell calling code that we have handled this event the buck
85 # stops here.
86 return gtk.TRUE
87 # Tell calling code that we have not handled this event pass it on.
88 return gtk.FALSE
89
90 # Print a string when a menu item is selected
91 def menuitem_response(self, widget, string):
92 print "%s" % string
93
94 def main():
95 gtk.main()
96 return 0
97
98 if __name__ == "__main__":
99 MenuExample()
100 main()
|
También puedes hacer un elemento de menú insensitivo y, usando una tabla de atajos, conectar teclas a retrollamadas de menús.