|
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="scale.py" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Escala (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#entry"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="grid.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="textview.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>Un widget deslizador para seleccionar un valor de un rango</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>Escala</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/scale.png"/>
|
|
Packit |
1470ea |
Desplace las escalas.
|
|
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="Scale Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(400, 300)
|
|
Packit |
1470ea |
self.set_border_width(5)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# two adjustments (initial value, min value, max value,
|
|
Packit |
1470ea |
# step increment - press cursor keys to see!,
|
|
Packit |
1470ea |
# page increment - click around the handle to see!,
|
|
Packit |
1470ea |
# page size - not used here)
|
|
Packit |
1470ea |
ad1 = Gtk.Adjustment(0, 0, 100, 5, 10, 0)
|
|
Packit |
1470ea |
ad2 = Gtk.Adjustment(50, 0, 100, 5, 10, 0)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# an horizontal scale
|
|
Packit |
1470ea |
self.h_scale = Gtk.Scale(
|
|
Packit |
1470ea |
orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1)
|
|
Packit |
1470ea |
# of integers (no digits)
|
|
Packit |
1470ea |
self.h_scale.set_digits(0)
|
|
Packit |
1470ea |
# that can expand horizontally if there is space in the grid (see
|
|
Packit |
1470ea |
# below)
|
|
Packit |
1470ea |
self.h_scale.set_hexpand(True)
|
|
Packit |
1470ea |
# that is aligned at the top of the space allowed in the grid (see
|
|
Packit |
1470ea |
# below)
|
|
Packit |
1470ea |
self.h_scale.set_valign(Gtk.Align.START)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we connect the signal "value-changed" emitted by the scale with the callback
|
|
Packit |
1470ea |
# function scale_moved
|
|
Packit |
1470ea |
self.h_scale.connect("value-changed", self.scale_moved)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a vertical scale
|
|
Packit |
1470ea |
self.v_scale = Gtk.Scale(
|
|
Packit |
1470ea |
orientation=Gtk.Orientation.VERTICAL, adjustment=ad2)
|
|
Packit |
1470ea |
# that can expand vertically if there is space in the grid (see below)
|
|
Packit |
1470ea |
self.v_scale.set_vexpand(True)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we connect the signal "value-changed" emitted by the scale with the callback
|
|
Packit |
1470ea |
# function scale_moved
|
|
Packit |
1470ea |
self.v_scale.connect("value-changed", self.scale_moved)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a label
|
|
Packit |
1470ea |
self.label = Gtk.Label()
|
|
Packit |
1470ea |
self.label.set_text("Move the scale handles...")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to attach the widgets
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.set_column_spacing(10)
|
|
Packit |
1470ea |
grid.set_column_homogeneous(True)
|
|
Packit |
1470ea |
grid.attach(self.h_scale, 0, 0, 1, 1)
|
|
Packit |
1470ea |
grid.attach_next_to(
|
|
Packit |
1470ea |
self.v_scale, self.h_scale, Gtk.PositionType.RIGHT, 1, 1)
|
|
Packit |
1470ea |
grid.attach(self.label, 0, 1, 2, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# any signal from the scales is signaled to the label the text of which is
|
|
Packit |
1470ea |
# changed
|
|
Packit |
1470ea |
def scale_moved(self, event):
|
|
Packit |
1470ea |
self.label.set_text("Horizontal scale is " + str(int(self.h_scale.get_value())) +
|
|
Packit |
1470ea |
"; vertical scale is " + str(self.v_scale.get_value()) + ".")
|
|
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 «Escala»</title>
|
|
Packit |
1470ea |
Se necesita un «Gtk.Adjustment» para construir la «Gtk.Scale». 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, página es el que se obtiene pulsando en la escala en sí. Tenga en cuenta que tamaño_de_página no se usa en este caso, y debe establecerse a 0 .
|
|
Packit |
1470ea |
En la línea 28, la señal «value-changed» se conecta a la función de retorno de llamada scale_moved() 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>get_value() obtiene el valor actual de la escala; set_value(valor) lo establece (si el valor , del tipo float , está fuera del rango mínimo o máximo, se ajustará para que entre). Estos son métodos de la clase «Gtk.Range». </item>
|
|
Packit |
1470ea |
<item>Use set_draw_value(False) para evitar mostrar el valor actual como una cadena junto al deslizador. </item>
|
|
Packit |
1470ea |
<item>Para resaltar la parte de la escala entre el origen y el valor actual:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.h_scale.set_restrict_to_fill_level(False)
|
|
Packit |
1470ea |
self.h_scale.set_fill_level(self.h_scale.get_value())
|
|
Packit |
1470ea |
self.h_scale.set_show_fill_level(True)
|
|
Packit |
1470ea |
en la función de retorno de llamada de la señal «value-changed», para tener el relleno nuevo cada vez que cambie el valor. Estos son métodos de la clase «Gtk.Range».
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>add_mark(valor, posición, marca) añade una marca en el valor (float o int si esa es la precisión de la escala), en posición (Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT, Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM ) con texto Null o marca en el lenguaje de marcado de Pango. Para limpiar las marcas, clear_marks() . </item>
|
|
Packit |
1470ea |
<item>set_digits(dígitos) establece la precisión de la escala en dígitos dígitos. </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/GtkScale.html">GtkScale</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkAdjustment.html">GtkAdjustment</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/gtk3-Standard-Enumerations.html">Enumeraciones estándar</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|