1<?xml version="1.0" encoding="utf-8"?> 2<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"> 3 <info> 4 <title type="text">Escala (Python)</title> 5 <link type="guide" xref="beginner.py#entry"/> 6 <link type="seealso" xref="grid.py"/> 7 <link type="next" xref="textview.py"/> 8 <revision version="0.2" date="2012-06-23" status="draft"/> 9 10 <credit type="author copyright"> 11 <name>Marta Maria Casetti</name> 12 <email its:translate="no">mmcasetti@gmail.com</email> 13 <years>2012</years> 14 </credit> 15 16 <desc>Un widget deslizador para seleccionar un valor de un rango</desc> 17 18 <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright"> 19 <mal:name>Daniel Mustieles</mal:name> 20 <mal:email>daniel.mustieles@gmail.com</mal:email> 21 <mal:years>2011 - 2020</mal:years> 22 </mal:credit> 23 24 <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright"> 25 <mal:name>Nicolás Satragno</mal:name> 26 <mal:email>nsatragno@gmail.com</mal:email> 27 <mal:years>2012 - 2013</mal:years> 28 </mal:credit> 29 30 <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright"> 31 <mal:name>Jorge González</mal:name> 32 <mal:email>jorgegonz@svn.gnome.org</mal:email> 33 <mal:years>2011</mal:years> 34 </mal:credit> 35 </info> 36 37 <title>Escala</title> 38 <media type="image" mime="image/png" src="media/scale.png"/> 39 <p>Desplace las escalas.</p> 40 41 <links type="section"/> 42 43 <section id="code"> 44 <title>Código usado para generar este ejemplo</title> 45 <code mime="text/x-python" style="numbered">from gi.repository import Gtk 46import sys 47 48 49class MyWindow(Gtk.ApplicationWindow): 50 51 def __init__(self, app): 52 Gtk.Window.__init__(self, title="Scale Example", application=app) 53 self.set_default_size(400, 300) 54 self.set_border_width(5) 55 56 # two adjustments (initial value, min value, max value, 57 # step increment - press cursor keys to see!, 58 # page increment - click around the handle to see!, 59 # page size - not used here) 60 ad1 = Gtk.Adjustment(0, 0, 100, 5, 10, 0) 61 ad2 = Gtk.Adjustment(50, 0, 100, 5, 10, 0) 62 63 # an horizontal scale 64 self.h_scale = Gtk.Scale( 65 orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1) 66 # of integers (no digits) 67 self.h_scale.set_digits(0) 68 # that can expand horizontally if there is space in the grid (see 69 # below) 70 self.h_scale.set_hexpand(True) 71 # that is aligned at the top of the space allowed in the grid (see 72 # below) 73 self.h_scale.set_valign(Gtk.Align.START) 74 75 # we connect the signal "value-changed" emitted by the scale with the callback 76 # function scale_moved 77 self.h_scale.connect("value-changed", self.scale_moved) 78 79 # a vertical scale 80 self.v_scale = Gtk.Scale( 81 orientation=Gtk.Orientation.VERTICAL, adjustment=ad2) 82 # that can expand vertically if there is space in the grid (see below) 83 self.v_scale.set_vexpand(True) 84 85 # we connect the signal "value-changed" emitted by the scale with the callback 86 # function scale_moved 87 self.v_scale.connect("value-changed", self.scale_moved) 88 89 # a label 90 self.label = Gtk.Label() 91 self.label.set_text("Move the scale handles...") 92 93 # a grid to attach the widgets 94 grid = Gtk.Grid() 95 grid.set_column_spacing(10) 96 grid.set_column_homogeneous(True) 97 grid.attach(self.h_scale, 0, 0, 1, 1) 98 grid.attach_next_to( 99 self.v_scale, self.h_scale, Gtk.PositionType.RIGHT, 1, 1) 100 grid.attach(self.label, 0, 1, 2, 1) 101 102 self.add(grid) 103 104 # any signal from the scales is signaled to the label the text of which is 105 # changed 106 def scale_moved(self, event): 107 self.label.set_text("Horizontal scale is " + str(int(self.h_scale.get_value())) + 108 "; vertical scale is " + str(self.v_scale.get_value()) + ".") 109 110 111class MyApplication(Gtk.Application): 112 113 def __init__(self): 114 Gtk.Application.__init__(self) 115 116 def do_activate(self): 117 win = MyWindow(self) 118 win.show_all() 119 120 def do_startup(self): 121 Gtk.Application.do_startup(self) 122 123app = MyApplication() 124exit_status = app.run(sys.argv) 125sys.exit(exit_status) 126</code> 127 </section> 128 129 <section id="methods"> 130 <title>Métodos útiles para un widget «Escala»</title> 131 <p>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 <code>Gtk.Adjustment(valor, mínimo, ,máximo, paso, página, tamaño_de_página)</code> donde los campos son del tipo <code>float</code>; <code>paso</code> es el incremento/decremento que se obtiene usando las teclas de dirección, <code>página</code> es el que se obtiene pulsando en la escala en sí. Tenga en cuenta que <code>tamaño_de_página</code> no se usa en este caso, y debe establecerse a <code>0</code>.</p> 132 <p>En la línea 28, la señal <code>«value-changed»</code> se conecta a la función de retorno de llamada <code>scale_moved()</code> usando <code><var>widget</var>.connect(<var>señal</var>, <var>función de retorno de llamada</var>)</code>. Consulte la sección <link xref="signals-callbacks.py"/> para una explicación más detallada.</p> 133 <list> 134 <item><p><code>get_value()</code> obtiene el valor actual de la escala; <code>set_value(valor)</code> lo establece (si el <code>valor</code>, del tipo <code>float</code>, está fuera del rango mínimo o máximo, se ajustará para que entre). Estos son métodos de la clase «Gtk.Range».</p></item> 135 <item><p>Use <code>set_draw_value(False)</code> para evitar mostrar el valor actual como una cadena junto al deslizador.</p></item> 136 <item><p>Para resaltar la parte de la escala entre el origen y el valor actual:</p> 137 <code mime="text/x-python"> 138self.h_scale.set_restrict_to_fill_level(False) 139self.h_scale.set_fill_level(self.h_scale.get_value()) 140self.h_scale.set_show_fill_level(True)</code> 141 <p>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».</p> 142 </item> 143 <item><p><code>add_mark(valor, posición, marca)</code> añade una marca en el <code>valor</code> (<code>float</code> o <code>int</code> si esa es la precisión de la escala), en <code>posición</code> (<code>Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT, Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM</code>) con texto <code>Null</code> o <code>marca</code> en el lenguaje de marcado de Pango. Para limpiar las marcas, <code>clear_marks()</code>.</p></item> 144 <item><p><code>set_digits(dígitos)</code> establece la precisión de la escala en <code>dígitos</code> dígitos.</p></item> 145 </list> 146 </section> 147 148 <section id="references"> 149 <title>Referencias de la API</title> 150 <p>En este ejemplo se usa lo siguiente:</p> 151 <list> 152 <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkScale.html">GtkScale</link></p></item> 153 <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkAdjustment.html">GtkAdjustment</link></p></item> 154 <item><p><link href="http://developer.gnome.org/gtk3/unstable/gtk3-Standard-Enumerations.html">Enumeraciones estándar</link></p></item> 155 </list> 156 </section> 157</page> 158