Skip to content

Commit 08b3901

Browse files
authored
Library: Port 'Spin Button' to Python (#756)
1 parent 4d63117 commit 08b3901

File tree

1 file changed

+35
-0
lines changed
  • src/Library/demos/Spin Button

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import gi
2+
3+
gi.require_version("Gtk", "4.0")
4+
from gi.repository import Gtk
5+
import workbench
6+
7+
hours: Gtk.SpinButton = workbench.builder.get_object("hours")
8+
minutes: Gtk.SpinButton = workbench.builder.get_object("minutes")
9+
10+
hours.set_text("00")
11+
minutes.set_text("00")
12+
13+
14+
def tell_time(hours, minutes):
15+
return f"The time selected is {hours.get_text()}:{minutes.get_text()}"
16+
17+
18+
def on_value_changed(spin_button):
19+
value = spin_button.get_adjustment().get_value()
20+
spin_button.set_text(f"{int(value):02}")
21+
return True
22+
23+
24+
hours.connect("value-changed", lambda *args: print(tell_time(hours, minutes)))
25+
minutes.connect("value-changed", lambda *args: print(tell_time(hours, minutes)))
26+
27+
28+
hours.connect("output", on_value_changed)
29+
30+
minutes.connect("output", on_value_changed)
31+
32+
33+
# This only works for one direction
34+
# Add any extra logic to account for wrapping in both directions
35+
minutes.connect("wrapped", lambda *args: hours.spin(Gtk.SpinType.STEP_FORWARD, 1))

0 commit comments

Comments
 (0)