Blame platform-demos/C/samples/scale.py

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)