3
# [SNIPPET_NAME: Spin Button]
4
# [SNIPPET_CATEGORIES: PyGTK]
5
# [SNIPPET_DESCRIPTION: Adding a spin button]
7
# example spinbutton.py
13
class SpinButtonExample:
14
def toggle_snap(self, widget, spin):
15
spin.set_snap_to_ticks(widget.get_active())
17
def toggle_numeric(self, widget, spin):
18
spin.set_numeric(widget.get_active())
20
def change_digits(self, widget, spin, spin1):
21
spin1.set_digits(spin.get_value_as_int())
23
def get_value(self, widget, data, spin, spin2, label):
25
buf = "%d" % spin.get_value_as_int()
27
buf = "%0.*f" % (spin2.get_value_as_int(),
32
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
33
window.connect("destroy", lambda w: gtk.main_quit())
34
window.set_title("Spin Button")
36
main_vbox = gtk.VBox(False, 5)
37
main_vbox.set_border_width(10)
40
frame = gtk.Frame("Not accelerated")
41
main_vbox.pack_start(frame, True, True, 0)
43
vbox = gtk.VBox(False, 0)
44
vbox.set_border_width(5)
47
# Day, month, year spinners
48
hbox = gtk.HBox(False, 0)
49
vbox.pack_start(hbox, True, True, 5)
51
vbox2 = gtk.VBox(False, 0)
52
hbox.pack_start(vbox2, True, True, 5)
54
label = gtk.Label("Day :")
55
label.set_alignment(0, 0.5)
56
vbox2.pack_start(label, False, True, 0)
58
adj = gtk.Adjustment(1.0, 1.0, 31.0, 1.0, 5.0, 0.0)
59
spinner = gtk.SpinButton(adj, 0, 0)
60
spinner.set_wrap(True)
61
vbox2.pack_start(spinner, False, True, 0)
63
vbox2 = gtk.VBox(False, 0)
64
hbox.pack_start(vbox2, True, True, 5)
66
label = gtk.Label("Month :")
67
label.set_alignment(0, 0.5)
68
vbox2.pack_start(label, False, True, 0)
70
adj = gtk.Adjustment(1.0, 1.0, 12.0, 1.0, 5.0, 0.0)
71
spinner = gtk.SpinButton(adj, 0, 0)
72
spinner.set_wrap(True)
73
vbox2.pack_start(spinner, False, True, 0)
75
vbox2 = gtk.VBox(False, 0)
76
hbox.pack_start(vbox2, True, True, 5)
78
label = gtk.Label("Year :")
79
label.set_alignment(0, 0.5)
80
vbox2.pack_start(label, False, True, 0)
82
adj = gtk.Adjustment(1998.0, 0.0, 2100.0, 1.0, 100.0, 0.0)
83
spinner = gtk.SpinButton(adj, 0, 0)
84
spinner.set_wrap(False)
85
spinner.set_size_request(55, -1)
86
vbox2.pack_start(spinner, False, True, 0)
88
frame = gtk.Frame("Accelerated")
89
main_vbox.pack_start(frame, True, True, 0)
91
vbox = gtk.VBox(False, 0)
92
vbox.set_border_width(5)
95
hbox = gtk.HBox(False, 0)
96
vbox.pack_start(hbox, False, True, 5)
98
vbox2 = gtk.VBox(False, 0)
99
hbox.pack_start(vbox2, True, True, 5)
101
label = gtk.Label("Value :")
102
label.set_alignment(0, 0.5)
103
vbox2.pack_start(label, False, True, 0)
105
adj = gtk.Adjustment(0.0, -10000.0, 10000.0, 0.5, 100.0, 0.0)
106
spinner1 = gtk.SpinButton(adj, 1.0, 2)
107
spinner1.set_wrap(True)
108
spinner1.set_size_request(100, -1)
109
vbox2.pack_start(spinner1, False, True, 0)
111
vbox2 = gtk.VBox(False, 0)
112
hbox.pack_start(vbox2, True, True, 5)
114
label = gtk.Label("Digits :")
115
label.set_alignment(0, 0.5)
116
vbox2.pack_start(label, False, True, 0)
118
adj = gtk.Adjustment(2, 1, 5, 1, 1, 0)
119
spinner2 = gtk.SpinButton(adj, 0.0, 0)
120
spinner2.set_wrap(True)
121
adj.connect("value_changed", self.change_digits, spinner2, spinner1)
122
vbox2.pack_start(spinner2, False, True, 0)
124
hbox = gtk.HBox(False, 0)
125
vbox.pack_start(hbox, False, True, 5)
127
button = gtk.CheckButton("Snap to 0.5-ticks")
128
button.connect("clicked", self.toggle_snap, spinner1)
129
vbox.pack_start(button, True, True, 0)
130
button.set_active(True)
132
button = gtk.CheckButton("Numeric only input mode")
133
button.connect("clicked", self.toggle_numeric, spinner1)
134
vbox.pack_start(button, True, True, 0)
135
button.set_active(True)
137
val_label = gtk.Label("")
139
hbox = gtk.HBox(False, 0)
140
vbox.pack_start(hbox, False, True, 5)
141
button = gtk.Button("Value as Int")
142
button.connect("clicked", self.get_value, 1, spinner1, spinner2,
144
hbox.pack_start(button, True, True, 5)
146
button = gtk.Button("Value as Float")
147
button.connect("clicked", self.get_value, 2, spinner1, spinner2,
149
hbox.pack_start(button, True, True, 5)
151
vbox.pack_start(val_label, True, True, 0)
152
val_label.set_text("0")
154
hbox = gtk.HBox(False, 0)
155
main_vbox.pack_start(hbox, False, True, 0)
157
button = gtk.Button("Close")
158
button.connect("clicked", lambda w: gtk.main_quit())
159
hbox.pack_start(button, True, True, 5)
166
if __name__ == "__main__":