/usr/share/doc/python-gtk2-tutorial/html/ch-GettingStarted.html is in python-gtk2-tutorial 2.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 2. Getting Started</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="index.html" title="PyGTK 2.0 Tutorial"><link rel="previous" href="ch-Introduction.html" title="Chapter 1. Introduction"><link rel="next" href="sec-TheoryOfSignalsAndCallbacks.html" title="2.2. Theory of Signals and Callbacks"></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">Chapter 2. Getting Started</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch-Introduction.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="sec-TheoryOfSignalsAndCallbacks.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="ch-GettingStarted"></a>Chapter 2. Getting Started</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch-GettingStarted.html#sec-HelloWorld">2.1. Hello World in PyGTK</a></span></dt><dt><span class="sect1"><a href="sec-TheoryOfSignalsAndCallbacks.html">2.2. Theory of Signals and Callbacks</a></span></dt><dt><span class="sect1"><a href="sec-Events.html">2.3. Events</a></span></dt><dt><span class="sect1"><a href="sec-SteppingThroughHelloWorld.html">2.4. Stepping Through Hello World</a></span></dt></dl></div><p>To begin our introduction to PyGTK, we'll start with the simplest
program possible. This program (<a href="examples/base.py" target="_top"><span><b class="command">base.py</b></span></a>) will create a
200x200 pixel window and has no way of exiting except to be killed by using
the shell.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
1 #!/usr/bin/env python
2
3 # example base.py
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8
9 class Base:
10 def __init__(self):
11 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
12 self.window.show()
13
14 def main(self):
15 gtk.main()
16
17 print __name__
18 if __name__ == "__main__":
19 base = Base()
20 base.main()
</pre></td></tr></table><p>You can run the above program using:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
python base.py
</pre></td></tr></table><p>If <a href="examples/base.py" target="_top"><span><b class="command">base.py</b></span></a> is made executable
and can be found in your <tt class="varname">PATH</tt>, it can be run
using:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
base.py
</pre></td></tr></table><p>Line 1 will invoke python to run <a href="examples/base.py" target="_top"><span><b class="command">base.py</b></span></a> in this
case. Lines 5-6 help differentiate between various versions of PyGTK that
may be installed on your system. These lines specify that we want to use
PyGTK version 2.0 which covers all versions of PyGTK with the major number
2. This prevents the program from using the earlier version of PyGTK if it
happens to be installed on your system. Lines 18-20 check if the
<tt class="varname">__name__</tt> variable is <tt class="literal">"__main__"</tt> which
indicates that the program is being run directly from python and not being
imported into a running python interpreter. In this case the program creates
a new instance of the Base class and saves a reference to it in the variable
base. It then invokes the method <tt class="methodname">main</tt>() to start
the GTK+ event processing loop.</p><p>A window similar to <a href="ch-GettingStarted.html#basefig" title="Figure 2.1. Simple PyGTK Window">Figure 2.1, “Simple PyGTK Window”</a> should popup on your
display.</p><div class="figure"><a name="basefig"></a><p class="title"><b>Figure 2.1. Simple PyGTK Window</b></p><div class="mediaobject" align="center"><img src="figures/base.png" align="middle" alt="Simple PyGTK Window"></div></div><p>The first line allows the program <a href="examples/base.py" target="_top"><span><b class="command">base.py</b></span></a> to be
invoked from a Linux or Unix shell program assuming that
<span><b class="command">python</b></span> is found your <tt class="varname">PATH</tt>. This line
will be the first line in all the example programs.</p><p>Lines 5-7 import the PyGTK 2 module and initializes the GTK+
environment. The PyGTK module defines the python interfaces to the GTK+
functions that will be used in the program. For those familiar with GTK+ the
initialization includes calling the <tt class="function">gtk_init</tt>() function.
This sets up a few things for us such as the
default visual and color map, default signal handlers, and checks the
arguments passed to your application on the command line, looking for one or
more of the following:</p><div class="itemizedlist"><ul type="disc"><li>--gtk-module</li><li>--g-fatal-warnings</li><li>--gtk-debug</li><li>--gtk-no-debug</li><li>--gdk-debug</li><li>--gdk-no-debug</li><li>--display</li><li>--sync</li><li>--name</li><li>--class</li></ul></div><p>It removes these from the argument list, leaving anything it does
not recognize for your application to parse or ignore. These are a set of
standard arguments accepted by all GTK+ applications.</p><p>Lines 9-15 define a python class named <tt class="classname">Base</tt>
that defines a class instance initialization method<tt class="methodname">
__init__</tt>(). The <tt class="methodname">__init__</tt>() function
creates a top level window (line 11) and directs GTK+ to display it (line
12). The <tt class="classname">gtk.Window</tt> is created in line 11 with the
argument <tt class="varname">gtk.WINDOW_TOPLEVEL</tt> that specifies that we want
the window to undergo window manager decoration and placement. Rather than
create a window of 0x0 size, a window without children is set to 200x200 by
default so you can still manipulate it.</p><p>Lines 14-15 define the <tt class="methodname">main</tt>() method that
calls the PyGTK <tt class="function">main</tt>() function that, in turn, invokes
the GTK+ main event processing loop to handle mouse and keyboard events as
well as window events.</p><p>Lines 18-20 allow the program to start automatically if called
directly or passed as an argument to the python interpreter; in these cases
the program name contained in the python variable
<tt class="varname">__name__</tt> will be the string <tt class="literal">"__main__"</tt>
and the code in lines 18-20 will be executed. If the program is loaded into
a running python interpreter using an import statement, lines 18-20 will not
be executed.</p><p>Line 19 creates an instance of the <tt class="classname">Base</tt>
class called base. A <tt class="classname">gtk.Window</tt> is created and
displayed as a result.</p><p>Line 20 calls the <tt class="methodname">main</tt>() method of the
<tt class="classname">Base</tt> class which starts the GTK+ event processing
loop. When control reaches this point, GTK+ will sleep waiting for X events
(such as button or key presses), timeouts, or file IO notifications to
occur. In our simple example, however, events are ignored.
</p><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-HelloWorld"></a>2.1. Hello World in PyGTK</h2></div></div><div></div></div><p>Now for a program with a widget (a button). It's the PyGTK
version of the classic hello world program (<a href="examples/helloworld.py" target="_top"><span><b class="command">helloworld.py</b></span></a>
).</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
1 #!/usr/bin/env python
2
3 # example helloworld.py
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8
9 class HelloWorld:
10
11 # This is a callback function. The data arguments are ignored
12 # in this example. More on callbacks below.
13 def hello(self, widget, data=None):
14 print "Hello World"
15
16 def delete_event(self, widget, event, data=None):
17 # If you return FALSE in the "delete_event" signal handler,
18 # GTK will emit the "destroy" signal. Returning TRUE means
19 # you don't want the window to be destroyed.
20 # This is useful for popping up 'are you sure you want to quit?'
21 # type dialogs.
22 print "delete event occurred"
23
24 # Change FALSE to TRUE and the main window will not be destroyed
25 # with a "delete_event".
26 return False
27
28 # Another callback
29 def destroy(self, widget, data=None):
30 gtk.main_quit()
31
32 def __init__(self):
33 # create a new window
34 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
35
36 # When the window is given the "delete_event" signal (this is given
37 # by the window manager, usually by the "close" option, or on the
38 # titlebar), we ask it to call the delete_event () function
39 # as defined above. The data passed to the callback
40 # function is NULL and is ignored in the callback function.
41 self.window.connect("delete_event", self.delete_event)
42
43 # Here we connect the "destroy" event to a signal handler.
44 # This event occurs when we call gtk_widget_destroy() on the window,
45 # or if we return FALSE in the "delete_event" callback.
46 self.window.connect("destroy", self.destroy)
47
48 # Sets the border width of the window.
49 self.window.set_border_width(10)
50
51 # Creates a new button with the label "Hello World".
52 self.button = gtk.Button("Hello World")
53
54 # When the button receives the "clicked" signal, it will call the
55 # function hello() passing it None as its argument. The hello()
56 # function is defined above.
57 self.button.connect("clicked", self.hello, None)
58
59 # This will cause the window to be destroyed by calling
60 # gtk_widget_destroy(window) when "clicked". Again, the destroy
61 # signal could come from here, or the window manager.
62 self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
63
64 # This packs the button into the window (a GTK container).
65 self.window.add(self.button)
66
67 # The final step is to display this newly created widget.
68 self.button.show()
69
70 # and the window
71 self.window.show()
72
73 def main(self):
74 # All PyGTK applications must have a gtk.main(). Control ends here
75 # and waits for an event to occur (like a key press or mouse event).
76 gtk.main()
77
78 # If the program is run directly or passed as an argument to the python
79 # interpreter then create a HelloWorld instance and show it
80 if __name__ == "__main__":
81 hello = HelloWorld()
82 hello.main()
</pre></td></tr></table><p><a href="ch-GettingStarted.html#helloworldfig" title="Figure 2.2. Hello World Example Program">Figure 2.2, “Hello World Example Program”</a> shows the window created by <a href="examples/helloworld.py" target="_top"><span><b class="command">helloworld.py</b></span></a>.</p><div class="figure"><a name="helloworldfig"></a><p class="title"><b>Figure 2.2. Hello World Example Program</b></p><div class="mediaobject" align="center"><img src="figures/helloworld.png" align="middle" alt="Hello World Example Program"></div></div><p> The variables and functions that are defined in the PyGTK
module are named as <tt class="literal">gtk.*</tt>. For example, the <a href="examples/helloworld.py" target="_top"><span><b class="command">helloworld.py</b></span></a>
program uses:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
False
gtk.mainquit()
gtk.Window()
gtk.Button()
</pre></td></tr></table><p>from the PyGTK module. In future sections I will not specify the
gtk module prefix but it will be assumed. The example programs will of
course use the module prefixes.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch-Introduction.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sec-TheoryOfSignalsAndCallbacks.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 2.2. Theory of Signals and Callbacks</td></tr></table></div></body></html>
|