The itemfactory.py example program uses the gtk.ItemFactory. Figure 11.2, “Item Factory Example” illustrates the program display:
The source code for itemfactory.py is:
1 #!/usr/bin/env python 2 3 # example itemfactory.py 4 5 import pygtk 6 pygtk.require('2.0') 7 import gtk 8 9 class ItemFactoryExample: 10 # Obligatory basic callback 11 def print_hello(self, w, data): 12 print "Hello, World!" 13 14 # This is the ItemFactoryEntry structure used to generate new menus. 15 # Item 1: The menu path. The letter after the underscore indicates an 16 # accelerator key once the menu is open. 17 # Item 2: The accelerator key for the entry 18 # Item 3: The callback. 19 # Item 4: The callback action. This changes the parameters with 20 # which the callback is called. The default is 0. 21 # Item 5: The item type, used to define what kind of an item it is. 22 # Here are the possible values: 23 24 # NULL -> "<Item>" 25 # "" -> "<Item>" 26 # "<Title>" -> create a title item 27 # "<Item>" -> create a simple item 28 # "<CheckItem>" -> create a check item 29 # "<ToggleItem>" -> create a toggle item 30 # "<RadioItem>" -> create a radio item 31 # <path> -> path of a radio item to link against 32 # "<Separator>" -> create a separator 33 # "<Branch>" -> create an item to hold sub items (optional) 34 # "<LastBranch>" -> create a right justified branch 35 36 def get_main_menu(self, window): 37 accel_group = gtk.AccelGroup() 38 39 # This function initializes the item factory. 40 # Param 1: The type of menu - can be MenuBar, Menu, 41 # or OptionMenu. 42 # Param 2: The path of the menu. 43 # Param 3: A reference to an AccelGroup. The item factory sets up 44 # the accelerator table while generating menus. 45 item_factory = gtk.ItemFactory(gtk.MenuBar, "<main>", accel_group) 46 47 # This method generates the menu items. Pass to the item factory 48 # the list of menu items 49 item_factory.create_items(self.menu_items) 50 51 # Attach the new accelerator group to the window. 52 window.add_accel_group(accel_group) 53 54 # need to keep a reference to item_factory to prevent its destruction 55 self.item_factory = item_factory 56 # Finally, return the actual menu bar created by the item factory. 57 return item_factory.get_widget("<main>") 58 59 def __init__(self): 60 self.menu_items = ( 61 ( "/_File", None, None, 0, "<Branch>" ), 62 ( "/File/_New", "<control>N", self.print_hello, 0, None ), 63 ( "/File/_Open", "<control>O", self.print_hello, 0, None ), 64 ( "/File/_Save", "<control>S", self.print_hello, 0, None ), 65 ( "/File/Save _As", None, None, 0, None ), 66 ( "/File/sep1", None, None, 0, "<Separator>" ), 67 ( "/File/Quit", "<control>Q", gtk.main_quit, 0, None ), 68 ( "/_Options", None, None, 0, "<Branch>" ), 69 ( "/Options/Test", None, None, 0, None ), 70 ( "/_Help", None, None, 0, "<LastBranch>" ), 71 ( "/_Help/About", None, None, 0, None ), 72 ) 73 window = gtk.Window(gtk.WINDOW_TOPLEVEL) 74 window.connect("destroy", lambda w: gtk.main_quit(), "WM destroy") 75 window.set_title("Item Factory") 76 window.set_size_request(300, 200) 77 78 main_vbox = gtk.VBox(False, 1) 79 main_vbox.set_border_width(1) 80 window.add(main_vbox) 81 main_vbox.show() 82 83 menubar = self.get_main_menu(window) 84 85 main_vbox.pack_start(menubar, False, True, 0) 86 menubar.show() 87 window.show() 88 89 def main(): 90 gtk.main() 91 return 0 92 93 if __name__ == "__main__": 94 ItemFactoryExample() 95 main() |
For now, there's only this example. An explanation and lots 'o' comments will follow later.