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="sv">
3  <info>
4    <title type="text">Scale (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>En skjutreglagekomponent för att välja ett värde från ett intervall</desc>
17
18    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
19      <mal:name>Sebastian Rasmussen</mal:name>
20      <mal:email>sebras@gmail.com</mal:email>
21      <mal:years>2019</mal:years>
22    </mal:credit>
23
24    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
25      <mal:name>Anders Jonsson</mal:name>
26      <mal:email>anders.jonsson@norsjovallen.se</mal:email>
27      <mal:years>2021</mal:years>
28    </mal:credit>
29  </info>
30
31  <title>Scale</title>
32  <media type="image" mime="image/png" src="media/scale.png"/>
33  <p>Slide the scales!</p>
34
35  <links type="section"/>
36
37  <section id="code">
38    <title>Kod som använts för att generera detta exempel</title>
39    <code mime="text/x-python" style="numbered">from gi.repository import Gtk
40import sys
41
42
43class MyWindow(Gtk.ApplicationWindow):
44
45    def __init__(self, app):
46        Gtk.Window.__init__(self, title="Scale Example", application=app)
47        self.set_default_size(400, 300)
48        self.set_border_width(5)
49
50        # two adjustments (initial value, min value, max value,
51        # step increment - press cursor keys to see!,
52        # page increment - click around the handle to see!,
53        # page size - not used here)
54        ad1 = Gtk.Adjustment(0, 0, 100, 5, 10, 0)
55        ad2 = Gtk.Adjustment(50, 0, 100, 5, 10, 0)
56
57        # an horizontal scale
58        self.h_scale = Gtk.Scale(
59            orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1)
60        # of integers (no digits)
61        self.h_scale.set_digits(0)
62        # that can expand horizontally if there is space in the grid (see
63        # below)
64        self.h_scale.set_hexpand(True)
65        # that is aligned at the top of the space allowed in the grid (see
66        # below)
67        self.h_scale.set_valign(Gtk.Align.START)
68
69        # we connect the signal "value-changed" emitted by the scale with the callback
70        # function scale_moved
71        self.h_scale.connect("value-changed", self.scale_moved)
72
73        # a vertical scale
74        self.v_scale = Gtk.Scale(
75            orientation=Gtk.Orientation.VERTICAL, adjustment=ad2)
76        # that can expand vertically if there is space in the grid (see below)
77        self.v_scale.set_vexpand(True)
78
79        # we connect the signal "value-changed" emitted by the scale with the callback
80        # function scale_moved
81        self.v_scale.connect("value-changed", self.scale_moved)
82
83        # a label
84        self.label = Gtk.Label()
85        self.label.set_text("Move the scale handles...")
86
87        # a grid to attach the widgets
88        grid = Gtk.Grid()
89        grid.set_column_spacing(10)
90        grid.set_column_homogeneous(True)
91        grid.attach(self.h_scale, 0, 0, 1, 1)
92        grid.attach_next_to(
93            self.v_scale, self.h_scale, Gtk.PositionType.RIGHT, 1, 1)
94        grid.attach(self.label, 0, 1, 2, 1)
95
96        self.add(grid)
97
98    # any signal from the scales is signaled to the label the text of which is
99    # changed
100    def scale_moved(self, event):
101        self.label.set_text("Horizontal scale is " + str(int(self.h_scale.get_value())) +
102                            "; vertical scale is " + str(self.v_scale.get_value()) + ".")
103
104
105class MyApplication(Gtk.Application):
106
107    def __init__(self):
108        Gtk.Application.__init__(self)
109
110    def do_activate(self):
111        win = MyWindow(self)
112        win.show_all()
113
114    def do_startup(self):
115        Gtk.Application.do_startup(self)
116
117app = MyApplication()
118exit_status = app.run(sys.argv)
119sys.exit(exit_status)
120</code>
121  </section>
122
123  <section id="methods">
124    <title>Användbara metoder för en Scale-komponent</title>
125    <p>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 <code>Gtk.Adjustment(value, lower, upper, step_increment, page_increment, page_size)</code> where the fields are of type <code>float</code>; <code>step_increment</code> is the increment/decrement that is obtained by using the cursor keys, <code>page_increment</code> the one that is obtained clicking on the scale itself. Note that <code>page_size</code> is not used in this case, it should be set to <code>0</code>.</p>
126    <p>På rad 28 ansluts signalen <code>"value-changed"</code> till återanropsfunktionen <code>scale_moved()</code> med <code><var>komponent</var>.connect(<var>signal</var>, <var>återanropsfunktion</var>)</code>. Se <link xref="signals-callbacks.py"/> för en utförligare förklaring.</p>
127    <list>
128      <item><p><code>get_value()</code> retrieves the current value of the scale; <code>set_value(value)</code> sets it (if the <code>value</code>, of type <code>float</code>, is outside the minimum or maximum range, it will be clamped to fit inside them). These are methods of the class Gtk.Range.</p></item>
129      <item><p>Use <code>set_draw_value(False)</code> to avoid displaying the current value as a string next to the slider.</p></item>
130      <item><p>To highlight the part of the scale between the origin and the current value:</p>
131        <code mime="text/x-python">
132self.h_scale.set_restrict_to_fill_level(False)
133self.h_scale.set_fill_level(self.h_scale.get_value())
134self.h_scale.set_show_fill_level(True)</code>
135        <p>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.</p>
136      </item>
137      <item><p><code>add_mark(value, position, markup)</code> adds a mark at the <code>value</code> (<code>float</code> or <code>int</code> if that is the precision of the scale), in <code>position</code> (<code>Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT, Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM</code>) with text <code>Null</code> or <code>markup</code> in the Pango Markup Language. To clear marks, <code>clear_marks()</code>.</p></item>
138      <item><p><code>set_digits(digits)</code> sets the precision of the scale at <code>digits</code> digits.</p></item>
139    </list>
140  </section>
141
142  <section id="references">
143    <title>API-referenser</title>
144    <p>I detta exempel använde vi följande:</p>
145    <list>
146      <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkScale.html">GtkScale</link></p></item>
147      <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkAdjustment.html">GtkAdjustment</link></p></item>
148      <item><p><link href="http://developer.gnome.org/gtk3/unstable/gtk3-Standard-Enumerations.html">Standarduppräkningstyper</link></p></item>
149    </list>
150  </section>
151</page>
152