|
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="spinbutton.py" xml:lang="pt-BR">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">SpinButton (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#entry"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="signals-callbacks.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="entry.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-06-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>Retrieve an integer or floating-point number from the user.</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Rafael Ferreira</mal:name>
|
|
Packit |
1470ea |
<mal:email>rafael.f.f1@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>SpinButton</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/spinbutton.png"/>
|
|
Packit |
1470ea |
Choose a number, by entering it or by clicking on the -/+ buttons!
|
|
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 |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="SpinButton Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(210, 70)
|
|
Packit |
1470ea |
self.set_border_width(5)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# an adjustment (initial value, min value, max value,
|
|
Packit |
1470ea |
# step increment - press cursor keys or +/- buttons to see!,
|
|
Packit |
1470ea |
# page increment - not used here,
|
|
Packit |
1470ea |
# page size - not used here)
|
|
Packit |
1470ea |
ad = Gtk.Adjustment(0, 0, 100, 1, 0, 0)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a spin button for integers (digits=0)
|
|
Packit |
1470ea |
self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
|
|
Packit |
1470ea |
# as wide as possible
|
|
Packit |
1470ea |
self.spin.set_hexpand(True)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we connect the signal "value-changed" emitted by the spinbutton with the callback
|
|
Packit |
1470ea |
# function spin_selected
|
|
Packit |
1470ea |
self.spin.connect("value-changed", self.spin_selected)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a label
|
|
Packit |
1470ea |
self.label = Gtk.Label()
|
|
Packit |
1470ea |
self.label.set_text("Choose a number")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to attach the widgets
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.attach(self.spin, 0, 0, 1, 1)
|
|
Packit |
1470ea |
grid.attach(self.label, 0, 1, 2, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function: the signal of the spinbutton is used to change the
|
|
Packit |
1470ea |
# text of the label
|
|
Packit |
1470ea |
def spin_selected(self, event):
|
|
Packit |
1470ea |
self.label.set_text(
|
|
Packit |
1470ea |
"The number you selected is " + str(self.spin.get_value_as_int()) + ".")
|
|
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 SpinButton widget</title>
|
|
Packit |
1470ea |
A Gtk.Adjustment is needed to construct the Gtk.SpinButton. This is the representation of a value with a lower and upper bound, together with step and page increments, and a page size, and it is constructed as Gtk.Adjustment(value, lower, upper, step_increment, page_increment, page_size) where the fields are of type float ; step_increment is the increment/decrement that is obtained by using the cursor keys or the buttons of the spinbutton. Note that page_increment and page_size are not used in this case, and they should be set to 0 .
|
|
Packit |
1470ea |
In line 23 the signal "value-changed" is connected to the callback function spin_selected() using widget.connect(signal, callback function) . See <link xref="signals-callbacks.py"/> for a more detailed explanation.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>If you want the value of the spinbutton to wrap around when they exceed the maximum or the minimum, set set_wrap(True) . The "wrapped" signal is emitted when this happens. </item>
|
|
Packit |
1470ea |
<item>set_digits(digits) sets the precision to be displayed by the spinbutton, up to 20 digits. </item>
|
|
Packit |
1470ea |
<item>To get the value of the spinbutton as an integer, use get_value_as_int() . </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API References</title>
|
|
Packit |
1470ea |
In this sample we used the following:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkSpinButton.html">GtkSpinButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkAdjustment.html">GtkAdjustment</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|