/usr/share/doc/python-gtk2-tutorial/html/sec-ToggleButtons.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 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>6.2. Toggle Buttons</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-ButtonWidget.html" title="Chapter 6. The Button Widget"><link rel="previous" href="ch-ButtonWidget.html" title="Chapter 6. The Button Widget"><link rel="next" href="sec-CheckButtons.html" title="6.3. Check Buttons"></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">6.2. Toggle Buttons</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch-ButtonWidget.html">Prev</a> </td><th width="60%" align="center">Chapter 6. The Button Widget</th><td width="20%" align="right"> <a accesskey="n" href="sec-CheckButtons.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-ToggleButtons"></a>6.2. Toggle Buttons</h2></div></div><div></div></div><p>Toggle buttons are derived from normal buttons and are very
similar, except they will always be in one of two states, alternated by a
click. They may be depressed, and when you click again, they will pop back
up. Click again, and they will pop back down.</p><p>Toggle buttons are the basis for check buttons and radio
buttons, as such, many of the calls used for toggle buttons are inherited by
radio and check buttons. I will point these out when we come to them.</p><p>Creating a new toggle button:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
toggle_button = gtk.ToggleButton(<b class="parameter"><tt>label</tt></b>=None)
</pre></td></tr></table><p>As you can imagine, these work identically to the normal button
widget calls. If no label is specified the button will be blank. The label
text will be parsed for '_'-prefixed mnemonic characters.</p><p>To retrieve the state of the toggle widget, including radio and
check buttons, we use a construct as shown in our example below. This tests
the state of the toggle, by calling the <tt class="methodname">get_active</tt>()
method of the toggle button object. The signal of interest to us that is
emitted by toggle buttons (the toggle button, check button, and radio button
widgets) is the "toggled" signal. To check the state of these buttons, set
up a signal handler to catch the toggled signal, and access the object
attributes to determine its state. The callback will look something
like:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
def toggle_button_callback(widget, data):
if widget.get_active():
# If control reaches here, the toggle button is down
else:
# If control reaches here, the toggle button is up
</pre></td></tr></table><p>To force the state of a toggle button, and its children, the
radio and check buttons, use this method:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
toggle_button.set_active(<b class="parameter"><tt>is_active</tt></b>)
</pre></td></tr></table><p>The above method can be used to set the state of the toggle
button, and its children the radio and check buttons. Specifying a
<tt class="literal">TRUE</tt> or <tt class="literal">FALSE</tt> for the
<i class="parameter"><tt>is_active</tt></i> argument indicates whether the button
should be down (depressed) or up (released). When the toggle button is
created its default is up or <tt class="literal">FALSE</tt>.</p><p>Note that when you use the <tt class="methodname">set_active</tt>()
method, and the state is actually changed, it causes the "clicked" and
"toggled" signals to be emitted from the button.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
toggle_button.get_active()
</pre></td></tr></table><p>This method returns the current state of the toggle button as a
boolean <tt class="literal">TRUE</tt> or <tt class="literal">FALSE</tt> value.</p><p>The <a href="examples/togglebutton.py" target="_top"><span><b class="command">togglebutton.py</b></span></a>
program provides a simple example using toggle buttons.
<a href="sec-ToggleButtons.html#togglefig" title="Figure 6.2. Toggle Button Example">Figure 6.2, “Toggle Button Example”</a> illustrates the resulting window with the second
toggle button active:</p><div class="figure"><a name="togglefig"></a><p class="title"><b>Figure 6.2. Toggle Button Example</b></p><div class="mediaobject" align="center"><img src="figures/toggle.png" align="middle" alt="Toggle Button Example"></div></div><p>The source code for the program is:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
1 #!/usr/bin/env python
2
3 # example togglebutton.py
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8
9 class ToggleButton:
10 # Our callback.
11 # The data passed to this method is printed to stdout
12 def callback(self, widget, data=None):
13 print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
14
15 # This callback quits the program
16 def delete_event(self, widget, event, data=None):
17 gtk.main_quit()
18 return False
19
20 def __init__(self):
21 # Create a new window
22 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
23
24 # Set the window title
25 self.window.set_title("Toggle Button")
26
27 # Set a handler for delete_event that immediately
28 # exits GTK.
29 self.window.connect("delete_event", self.delete_event)
30
31 # Sets the border width of the window.
32 self.window.set_border_width(20)
33
34 # Create a vertical box
35 vbox = gtk.VBox(True, 2)
36
37 # Put the vbox in the main window
38 self.window.add(vbox)
39
40 # Create first button
41 button = gtk.ToggleButton("toggle button 1")
42
43 # When the button is toggled, we call the "callback" method
44 # with a pointer to "button" as its argument
45 button.connect("toggled", self.callback, "toggle button 1")
46
47
48 # Insert button 1
49 vbox.pack_start(button, True, True, 2)
50
51 button.show()
52
53 # Create second button
54
55 button = gtk.ToggleButton("toggle button 2")
56
57 # When the button is toggled, we call the "callback" method
58 # with a pointer to "button 2" as its argument
59 button.connect("toggled", self.callback, "toggle button 2")
60 # Insert button 2
61 vbox.pack_start(button, True, True, 2)
62
63 button.show()
64
65 # Create "Quit" button
66 button = gtk.Button("Quit")
67
68 # When the button is clicked, we call the main_quit function
69 # and the program exits
70 button.connect("clicked", lambda wid: gtk.main_quit())
71
72 # Insert the quit button
73 vbox.pack_start(button, True, True, 2)
74
75 button.show()
76 vbox.show()
77 self.window.show()
78
79 def main():
80 gtk.main()
81 return 0
82
83 if __name__ == "__main__":
84 ToggleButton()
85 main()
</pre></td></tr></table><p>The interesting lines are 12-13 which define the
<tt class="methodname">callback</tt>() method that prints the toggle button
label and its state when it is toggled. Lines 45 and 59 connect the
"toggled" signal of the toggle buttons to the
<tt class="methodname">callback</tt>() method.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch-ButtonWidget.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch-ButtonWidget.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sec-CheckButtons.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. The Button Widget </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 6.3. Check Buttons</td></tr></table></div></body></html>
|