|
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="button.py" xml:lang="de">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Knopf (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#buttons"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="signals-callbacks.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="linkbutton.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-05-05" 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 widget which emits a signal when clicked</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>Knopf</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/button.png"/>
|
|
Packit |
1470ea |
A button widget connected to a simple callback function.
|
|
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 |
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__(self, title="GNOME Button", application=app)
|
|
Packit |
1470ea |
self.set_default_size(250, 50)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a button
|
|
Packit |
1470ea |
button = Gtk.Button()
|
|
Packit |
1470ea |
# with a label
|
|
Packit |
1470ea |
button.set_label("Click me")
|
|
Packit |
1470ea |
# connect the signal "clicked" emitted by the button
|
|
Packit |
1470ea |
# to the callback function do_clicked
|
|
Packit |
1470ea |
button.connect("clicked", self.do_clicked)
|
|
Packit |
1470ea |
# add the button to the window
|
|
Packit |
1470ea |
self.add(button)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function connected to the signal "clicked" of the button
|
|
Packit |
1470ea |
def do_clicked(self, button):
|
|
Packit |
1470ea |
print("You clicked me!")
|
|
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 |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Useful methods for a Button widget</title>
|
|
Packit |
1470ea |
In line 16 the "clicked" signal from the button is connected to the callback function do_clicked() using widget.connect(signal, callback function) . See <link xref="signals-callbacks.py"/> for a more detailed explanation.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>set_relief(Gtk.ReliefStyle.NONE) sets to none the relief style of the edges of the Gtk.Button - as opposed to Gtk.ReliefStyle.NORMAL . </item>
|
|
Packit |
1470ea |
<item>If the label of the button is a <link href="http://developer.gnome.org/gtk3/unstable/gtk3-Stock-Items.html">stock icon</link>, set_use_stock(True) sets the label as the name of the corresponding stock icon. </item>
|
|
Packit |
1470ea |
<item>To set an image (e.g. a stock image) for the button button :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
image = Gtk.Image()
|
|
Packit |
1470ea |
image.set_from_stock(Gtk.STOCK_ABOUT, Gtk.IconSize.BUTTON)
|
|
Packit |
1470ea |
button.set_image(image)
|
|
Packit |
1470ea |
You should not set a label for the button after this, otherwise it will show the label and not the image. </item>
|
|
Packit |
1470ea |
<item>If we use set_focus_on_click(False) the button will not grab focus when it is clicked by the mouse. This could be useful in places like toolbars, so that the keyboard focus is not removed from the main area of the application. </item>
|
|
Packit |
1470ea |
</list>
|
|
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/GtkButton.html">GtkButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkWindow.html">GtkWindow</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|