File: sec-ItemFactoryExample.html

package info (click to toggle)
python-gtk2-tutorial 2.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,376 kB
  • ctags: 918
  • sloc: python: 5,731; makefile: 36
file content (101 lines) | stat: -rw-r--r-- 7,267 bytes parent folder | download
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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>11.4.Item Factory Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="PyGTK 2.0 Tutorial"><link rel="up" href="ch-MenuWidget.html" title="Chapter11.Menu Widget"><link rel="previous" href="sec-UsingItemFactory.html" title="11.3.Using ItemFactory"><link rel="next" href="ch-DrawingArea.html" title="Chapter12.Drawing Area"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">11.4.Item Factory Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="sec-UsingItemFactory.html">Prev</a></td><th width="60%" align="center">Chapter11.Menu Widget</th><td width="20%" align="right"><a accesskey="n" href="ch-DrawingArea.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-ItemFactoryExample"></a>11.4.Item Factory Example</h2></div></div><div></div></div><p>The <a href="examples/itemfactory.py" target="_top"><span><b class="command">itemfactory.py</b></span></a>
example program uses the <tt class="classname">gtk.ItemFactory</tt>.
<a href="sec-ItemFactoryExample.html#itemfactoryfig" title="Figure11.2.Item Factory Example">Figure11.2, &#8220;Item Factory Example&#8221;</a> illustrates the program display:</p><div class="figure"><a name="itemfactoryfig"></a><p class="title"><b>Figure11.2.Item Factory Example</b></p><div class="mediaobject" align="center"><img src="figures/itemfactory.png" align="middle" alt="Item Factory Example"></div></div><p>The source code for <a href="examples/itemfactory.py" target="_top"><span><b class="command">itemfactory.py</b></span></a>
is:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
    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               -&gt; "&lt;Item&gt;"
   25	    #       ""                 -&gt; "&lt;Item&gt;"
   26	    #       "&lt;Title&gt;"          -&gt; create a title item
   27	    #       "&lt;Item&gt;"           -&gt; create a simple item
   28	    #       "&lt;CheckItem&gt;"      -&gt; create a check item
   29	    #       "&lt;ToggleItem&gt;"     -&gt; create a toggle item
   30	    #       "&lt;RadioItem&gt;"      -&gt; create a radio item
   31	    #       &lt;path&gt;             -&gt; path of a radio item to link against
   32	    #       "&lt;Separator&gt;"      -&gt; create a separator
   33	    #       "&lt;Branch&gt;"         -&gt; create an item to hold sub items (optional)
   34	    #       "&lt;LastBranch&gt;"     -&gt; 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, "&lt;main&gt;", 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("&lt;main&gt;")
   58	
   59	    def __init__(self):
   60	        self.menu_items = (
   61	            ( "/_File",         None,         None, 0, "&lt;Branch&gt;" ),
   62	            ( "/File/_New",     "&lt;control&gt;N", self.print_hello, 0, None ),
   63	            ( "/File/_Open",    "&lt;control&gt;O", self.print_hello, 0, None ),
   64	            ( "/File/_Save",    "&lt;control&gt;S", self.print_hello, 0, None ),
   65	            ( "/File/Save _As", None,         None, 0, None ),
   66	            ( "/File/sep1",     None,         None, 0, "&lt;Separator&gt;" ),
   67	            ( "/File/Quit",     "&lt;control&gt;Q", gtk.main_quit, 0, None ),
   68	            ( "/_Options",      None,         None, 0, "&lt;Branch&gt;" ),
   69	            ( "/Options/Test",  None,         None, 0, None ),
   70	            ( "/_Help",         None,         None, 0, "&lt;LastBranch&gt;" ),
   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(gtk.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, gtk.FALSE, gtk.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()
</pre></td></tr></table><p>For now, there's only this example. An explanation and lots 'o'
comments will follow later.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sec-UsingItemFactory.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ch-MenuWidget.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch-DrawingArea.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.3.Using ItemFactory</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Chapter12.Drawing Area</td></tr></table></div></body></html>