Blame platform-demos/gl/scale.py.page

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="gl">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">Scale (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 de desprazamento para seleccionar un valor dun rango</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Fran Dieguez</mal:name>
Packit 1470ea
      <mal:email>frandieguez@gnome.org</mal:email>
Packit 1470ea
      <mal:years>2012-2013.</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
  

Desprazar as escalas!

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Código usado para xerar este exemplo</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 o widget Scale</title>
Packit 1470ea
    

A Gtk.Adjustment is needed to construct the Gtk.Scale. 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, page_increment the one that is obtained clicking on the scale itself. Note that page_size is not used in this case, it should be set to 0.

Packit 1470ea
    

In line 28 the signal "value-changed" is connected to the callback function scale_moved() using widget.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

Packit 1470ea
    <list>
Packit 1470ea
      <item>

get_value() retrieves the current value of the scale; set_value(value) sets it (if the value, of type float, is outside the minimum or maximum range, it will be clamped to fit inside them). These are methods of the class Gtk.Range.

</item>
Packit 1470ea
      <item>

Use set_draw_value(False) to avoid displaying the current value as a string next to the slider.

</item>
Packit 1470ea
      <item>

To highlight the part of the scale between the origin and the current value:

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
        

in the callback function of the "value-changed" signal, so to have the new filling every time the value is changed. These are methods of the class Gtk.Range.

Packit 1470ea
      </item>
Packit 1470ea
      <item>

add_mark(value, position, markup) adds a mark at the value (float or int if that is the precision of the scale), in position (Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT, Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM) with text Null or markup in the Pango Markup Language. To clear marks, clear_marks().

</item>
Packit 1470ea
      <item>

set_digits(digits) sets the precision of the scale at digits digits.

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API References</title>
Packit 1470ea
    

Neste exemplo empregaremos o seguinte:

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">Standard Enumerations</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>