|
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="es">
|
|
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>Obtener un número entero o en coma flotante del usuario.</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Daniel Mustieles</mal:name>
|
|
Packit |
1470ea |
<mal:email>daniel.mustieles@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011 - 2017</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Nicolás Satragno</mal:name>
|
|
Packit |
1470ea |
<mal:email>nsatragno@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012 - 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Jorge González</mal:name>
|
|
Packit |
1470ea |
<mal:email>jorgegonz@svn.gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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 |
Elija un número, escribiéndolo o pulsando los botones -/+.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para generar este ejemplo</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>Métodos útiles para un widget «SpinButton»</title>
|
|
Packit |
1470ea |
Se necesita un «Gtk.Adjustment» para construir el «Gtk.SpinButton». Este es la representación de un valor con un límite superior e inferior, junto con pasos y páginas de incrementos, y un tamaño de página, y se construye como Gtk.Adjustment(valor, mínimo, ,máximo, paso, página, tamaño_de_página) donde los campos son del tipo float ; paso es el incremento/decremento que se obtiene usando las teclas de dirección o los botones del botón incremental. Tenga en cuenta que página y tamaño_de_página no se usan en este caso, y deben establecerse a 0 .
|
|
Packit |
1470ea |
En la línea 23, la señal «value-changed» se conecta a la función de retorno de llamada spin_selected() usando widget.connect(señal, función de retorno de llamada) . Consulte la <link xref="signals-callbacks.py"/> para una explicación más detallada.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>Si quiere que el valor del botón incremental dé la vuelta cuando exceda el máximo o el mínimo, establezca set_wrap(True) . La señal "wrapped" se emite cuando esto sucede. </item>
|
|
Packit |
1470ea |
<item>set_digits(dígitos) establece la precisión que muestra el botón incremental, hasta 20 dígitos. </item>
|
|
Packit |
1470ea |
<item>Para obtener el valor del botón incremental como un entero, use get_value_as_int() . </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Referencias de la API</title>
|
|
Packit |
1470ea |
En este ejemplo se usa lo siguiente:
|
|
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>
|