|
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="fr">
|
|
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 élément graphique glissière pour choisir une valeur dans un intervalle</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Luc Rebert,</mal:name>
|
|
Packit |
1470ea |
<mal:email>traduc@rebert.name</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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>Alain Lojewski,</mal:name>
|
|
Packit |
1470ea |
<mal:email>allomervan@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011-2012</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>Luc Pionchon</mal:name>
|
|
Packit |
1470ea |
<mal:email>pionchon.luc@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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>Bruno Brouard</mal:name>
|
|
Packit |
1470ea |
<mal:email>annoa.b@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011-12</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>Luis Menina</mal:name>
|
|
Packit |
1470ea |
<mal:email>liberforce@freeside.fr</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Échelle</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/scale.png"/>
|
|
Packit |
1470ea |
Faites glisser les échelles !
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Code utilisé pour générer cet exemple</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éthodes utiles pour un élément graphique échelle</title>
|
|
Packit |
1470ea |
Pour construire une échelle Gtk.Scale, il faut un Gtk.Adjustment. C'est la représentation d'une valeur avec une limite inférieure et supérieure, des incréments d'étape et de page, d'une taille de page et est construite sous la forme Gtk.Adjustment(valeur, inferieure, superieure, step_increment, page_increment, page_size) , où les champs sont du type flottant float ; step_increment est l'incrément ou décrément obtenu par l'utilisation des touches fléchées et page_increment est celui obtenu en cliquant sur l'échelle elle-même. Notez que la taille page_size n'est pas utilisée dans notre exemple et que sa valeur doit être définie à 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>La méthode get_value() récupère la valeur actuelle de l'échelle ; set_value(valeur) la définit (si la valeur , du type float , est en dehors des limites inférieure ou supérieure, elle sera rétrécie pour tenir à l'intérieur). Ce sont les méthodes de la classe Gtk.Range. </item>
|
|
Packit |
1470ea |
<item>Utilisez set_draw_value(faux) pour éviter d'afficher la valeur actuelle sous forme d'une chaîne à côté de la glissière. </item>
|
|
Packit |
1470ea |
<item>Pour surligner la partie d'échelle située entre l'origine et la valeur actuelle :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.h_scale.set_restrict_to_fill_level(faux)
|
|
Packit |
1470ea |
self.h_scale.set_fill_level(self.h_scale.get_value())
|
|
Packit |
1470ea |
self.h_scale.set_show_fill_level(vrai)
|
|
Packit |
1470ea |
dans la fonction de rappel du signal « value-changed », pour avoir le nouveau remplissage chaque fois que la valeur est modifiée. Ce sont les méthodes de la classe Gtk.Range.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>La méthode add_mark(valeur, position, marqueur) ajoute un marqueur à la valeur (float ou int selon la précision de l'échelle) dans position (Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT, Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM ) avec le texte Null ou marqueur en langage Pango Markup. La fonction clear_marks() supprime les marqueurs. </item>
|
|
Packit |
1470ea |
<item>La méthode set_digits(digits) définit la précision de l'échelle aux caractères digits . </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Références API</title>
|
|
Packit |
1470ea |
Dans cet exemple, les éléments suivants sont utilisés :
|
|
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>
|