|
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="SpinButton Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(210, 70)
|
|
Packit |
1470ea |
self.set_border_width(5)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# an adjustment (initial value, min value, max value,
|
|
Packit |
1470ea |
# step increment - press cursor keys or +/- buttons to see!,
|
|
Packit |
1470ea |
# page increment - not used here,
|
|
Packit |
1470ea |
# page size - not used here)
|
|
Packit |
1470ea |
ad = Gtk.Adjustment(0, 0, 100, 1, 0, 0)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a spin button for integers (digits=0)
|
|
Packit |
1470ea |
self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
|
|
Packit |
1470ea |
# as wide as possible
|
|
Packit |
1470ea |
self.spin.set_hexpand(True)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we connect the signal "value-changed" emitted by the spinbutton with the callback
|
|
Packit |
1470ea |
# function spin_selected
|
|
Packit |
1470ea |
self.spin.connect("value-changed", self.spin_selected)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a label
|
|
Packit |
1470ea |
self.label = Gtk.Label()
|
|
Packit |
1470ea |
self.label.set_text("Choose a number")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to attach the widgets
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.attach(self.spin, 0, 0, 1, 1)
|
|
Packit |
1470ea |
grid.attach(self.label, 0, 1, 2, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function: the signal of the spinbutton is used to change the
|
|
Packit |
1470ea |
# text of the label
|
|
Packit |
1470ea |
def spin_selected(self, event):
|
|
Packit |
1470ea |
self.label.set_text(
|
|
Packit |
1470ea |
"The number you selected is " + str(self.spin.get_value_as_int()) + ".")
|
|
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)
|