|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="togglebutton.py" xml:lang="de">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">ToggleButton (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#buttons"/>
|
|
Packit |
1470ea |
<link type="next" xref="switch.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-05-23" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A button which retains state</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Mario Blättermann</mal:name>
|
|
Packit |
1470ea |
<mal:email>mario.blaettermann@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011, 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>ToggleButton</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/togglebutton.png"/>
|
|
Packit |
1470ea |
When this ToggleButton is in an active state, the spinner spins.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Code used to generate this example</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
# a window
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(
|
|
Packit |
1470ea |
self, title="ToggleButton Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(300, 300)
|
|
Packit |
1470ea |
self.set_border_width(30)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a spinner animation
|
|
Packit |
1470ea |
self.spinner = Gtk.Spinner()
|
|
Packit |
1470ea |
# with extra horizontal space
|
|
Packit |
1470ea |
self.spinner.set_hexpand(True)
|
|
Packit |
1470ea |
# with extra vertical space
|
|
Packit |
1470ea |
self.spinner.set_vexpand(True)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a togglebutton
|
|
Packit |
1470ea |
button = Gtk.ToggleButton.new_with_label("Start/Stop")
|
|
Packit |
1470ea |
# connect the signal "toggled" emitted by the togglebutton
|
|
Packit |
1470ea |
# when its state is changed to the callback function toggled_cb
|
|
Packit |
1470ea |
button.connect("toggled", self.toggled_cb)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to allocate the widgets
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.set_row_homogeneous(False)
|
|
Packit |
1470ea |
grid.set_row_spacing(15)
|
|
Packit |
1470ea |
grid.attach(self.spinner, 0, 0, 1, 1)
|
|
Packit |
1470ea |
grid.attach(button, 0, 1, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the grid to the window
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for the signal "toggled"
|
|
Packit |
1470ea |
def toggled_cb(self, button):
|
|
Packit |
1470ea |
# if the togglebutton is active, start the spinner
|
|
Packit |
1470ea |
if button.get_active():
|
|
Packit |
1470ea |
self.spinner.start()
|
|
Packit |
1470ea |
# else, stop it
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.spinner.stop()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Useful methods for a ToggleButton widget</title>
|
|
Packit |
1470ea |
In line 22 the signal "toggled" is connected to the callback function toggled_cb() using widget.connect(signal, callback function) . See <link xref="signals-callbacks.py"/> for a more detailed explanation.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API-Referenzen</title>
|
|
Packit |
1470ea |
In this sample we used the following:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkToggleButton.html">GtkToggleButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkWidget.html">GtkWidget</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkSpinner.html">GtkSpinner</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|