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="de"> 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>A slider widget for selecting a value from a range</desc> 17 18 <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright"> 19 <mal:name>Mario Blättermann</mal:name> 20 <mal:email>mario.blaettermann@gmail.com</mal:email> 21 <mal:years>2011, 2013, 2016, 2018, 2021</mal:years> 22 </mal:credit> 23 </info> 24 25 <title>Scale</title> 26 <media type="image" mime="image/png" src="media/scale.png"/> 27 <p>Slide the scales!</p> 28 29 <links type="section"/> 30 31 <section id="code"> 32 <title>Code, der zum Generieren dieses Beispiels verwendet wurde</title> 33 <code mime="text/x-python" style="numbered">from gi.repository import Gtk 34import sys 35 36 37class MyWindow(Gtk.ApplicationWindow): 38 39 def __init__(self, app): 40 Gtk.Window.__init__(self, title="Scale Example", application=app) 41 self.set_default_size(400, 300) 42 self.set_border_width(5) 43 44 # two adjustments (initial value, min value, max value, 45 # step increment - press cursor keys to see!, 46 # page increment - click around the handle to see!, 47 # page size - not used here) 48 ad1 = Gtk.Adjustment(0, 0, 100, 5, 10, 0) 49 ad2 = Gtk.Adjustment(50, 0, 100, 5, 10, 0) 50 51 # an horizontal scale 52 self.h_scale = Gtk.Scale( 53 orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1) 54 # of integers (no digits) 55 self.h_scale.set_digits(0) 56 # that can expand horizontally if there is space in the grid (see 57 # below) 58 self.h_scale.set_hexpand(True) 59 # that is aligned at the top of the space allowed in the grid (see 60 # below) 61 self.h_scale.set_valign(Gtk.Align.START) 62 63 # we connect the signal "value-changed" emitted by the scale with the callback 64 # function scale_moved 65 self.h_scale.connect("value-changed", self.scale_moved) 66 67 # a vertical scale 68 self.v_scale = Gtk.Scale( 69 orientation=Gtk.Orientation.VERTICAL, adjustment=ad2) 70 # that can expand vertically if there is space in the grid (see below) 71 self.v_scale.set_vexpand(True) 72 73 # we connect the signal "value-changed" emitted by the scale with the callback 74 # function scale_moved 75 self.v_scale.connect("value-changed", self.scale_moved) 76 77 # a label 78 self.label = Gtk.Label() 79 self.label.set_text("Move the scale handles...") 80 81 # a grid to attach the widgets 82 grid = Gtk.Grid() 83 grid.set_column_spacing(10) 84 grid.set_column_homogeneous(True) 85 grid.attach(self.h_scale, 0, 0, 1, 1) 86 grid.attach_next_to( 87 self.v_scale, self.h_scale, Gtk.PositionType.RIGHT, 1, 1) 88 grid.attach(self.label, 0, 1, 2, 1) 89 90 self.add(grid) 91 92 # any signal from the scales is signaled to the label the text of which is 93 # changed 94 def scale_moved(self, event): 95 self.label.set_text("Horizontal scale is " + str(int(self.h_scale.get_value())) + 96 "; vertical scale is " + str(self.v_scale.get_value()) + ".") 97 98 99class MyApplication(Gtk.Application): 100 101 def __init__(self): 102 Gtk.Application.__init__(self) 103 104 def do_activate(self): 105 win = MyWindow(self) 106 win.show_all() 107 108 def do_startup(self): 109 Gtk.Application.do_startup(self) 110 111app = MyApplication() 112exit_status = app.run(sys.argv) 113sys.exit(exit_status) 114</code> 115 </section> 116 117 <section id="methods"> 118 <title>Nützliche Methoden für ein Scale-Widget</title> 119 <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> 120 <p>In line 28 the signal <code>"value-changed"</code> is connected to the callback function <code>scale_moved()</code> using <code><var>widget</var>.connect(<var>signal</var>, <var>callback function</var>)</code>. See <link xref="signals-callbacks.py"/> for a more detailed explanation.</p> 121 <list> 122 <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> 123 <item><p>Use <code>set_draw_value(False)</code> to avoid displaying the current value as a string next to the slider.</p></item> 124 <item><p>To highlight the part of the scale between the origin and the current value:</p> 125 <code mime="text/x-python"> 126self.h_scale.set_restrict_to_fill_level(False) 127self.h_scale.set_fill_level(self.h_scale.get_value()) 128self.h_scale.set_show_fill_level(True)</code> 129 <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> 130 </item> 131 <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> 132 <item><p><code>set_digits(digits)</code> sets the precision of the scale at <code>digits</code> digits.</p></item> 133 </list> 134 </section> 135 136 <section id="references"> 137 <title>API-Referenzen</title> 138 <p>In diesem Beispiel haben wir Folgendes verwendet:</p> 139 <list> 140 <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkScale.html">GtkScale</link></p></item> 141 <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkAdjustment.html">GtkAdjustment</link></p></item> 142 <item><p><link href="http://developer.gnome.org/gtk3/unstable/gtk3-Standard-Enumerations.html">Standard-Aufzählungen</link></p></item> 143 </list> 144 </section> 145</page> 146