~doctormo/python-snippets/lp-merge-request-example

« back to all changes in this revision

Viewing changes to pygtk/spinbutton.py

  • Committer: Jono Bacon
  • Date: 2009-12-31 01:32:01 UTC
  • Revision ID: jono@ubuntu.com-20091231013201-0jqe2hpla824dafl
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Spin Button]
 
4
# [SNIPPET_CATEGORIES: PyGTK]
 
5
# [SNIPPET_DESCRIPTION: Adding a spin button]
 
6
 
 
7
# example spinbutton.py
 
8
 
 
9
import pygtk
 
10
pygtk.require('2.0')
 
11
import gtk
 
12
 
 
13
class SpinButtonExample:
 
14
    def toggle_snap(self, widget, spin):
 
15
        spin.set_snap_to_ticks(widget.get_active())
 
16
 
 
17
    def toggle_numeric(self, widget, spin):
 
18
        spin.set_numeric(widget.get_active())
 
19
 
 
20
    def change_digits(self, widget, spin, spin1):
 
21
        spin1.set_digits(spin.get_value_as_int())
 
22
 
 
23
    def get_value(self, widget, data, spin, spin2, label):
 
24
        if data == 1:
 
25
            buf = "%d" % spin.get_value_as_int()
 
26
        else:
 
27
            buf = "%0.*f" % (spin2.get_value_as_int(),
 
28
                             spin.get_value())
 
29
        label.set_text(buf)
 
30
 
 
31
    def __init__(self):
 
32
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
33
        window.connect("destroy", lambda w: gtk.main_quit())
 
34
        window.set_title("Spin Button")
 
35
 
 
36
        main_vbox = gtk.VBox(False, 5)
 
37
        main_vbox.set_border_width(10)
 
38
        window.add(main_vbox)
 
39
 
 
40
        frame = gtk.Frame("Not accelerated")
 
41
        main_vbox.pack_start(frame, True, True, 0)
 
42
  
 
43
        vbox = gtk.VBox(False, 0)
 
44
        vbox.set_border_width(5)
 
45
        frame.add(vbox)
 
46
 
 
47
        # Day, month, year spinners
 
48
        hbox = gtk.HBox(False, 0)
 
49
        vbox.pack_start(hbox, True, True, 5)
 
50
  
 
51
        vbox2 = gtk.VBox(False, 0)
 
52
        hbox.pack_start(vbox2, True, True, 5)
 
53
 
 
54
        label = gtk.Label("Day :")
 
55
        label.set_alignment(0, 0.5)
 
56
        vbox2.pack_start(label, False, True, 0)
 
57
  
 
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)
 
62
  
 
63
        vbox2 = gtk.VBox(False, 0)
 
64
        hbox.pack_start(vbox2, True, True, 5)
 
65
  
 
66
        label = gtk.Label("Month :")
 
67
        label.set_alignment(0, 0.5)
 
68
        vbox2.pack_start(label, False, True, 0)
 
69
 
 
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)
 
74
  
 
75
        vbox2 = gtk.VBox(False, 0)
 
76
        hbox.pack_start(vbox2, True, True, 5)
 
77
  
 
78
        label = gtk.Label("Year :")
 
79
        label.set_alignment(0, 0.5)
 
80
        vbox2.pack_start(label, False, True, 0)
 
81
  
 
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)
 
87
  
 
88
        frame = gtk.Frame("Accelerated")
 
89
        main_vbox.pack_start(frame, True, True, 0)
 
90
  
 
91
        vbox = gtk.VBox(False, 0)
 
92
        vbox.set_border_width(5)
 
93
        frame.add(vbox)
 
94
  
 
95
        hbox = gtk.HBox(False, 0)
 
96
        vbox.pack_start(hbox, False, True, 5)
 
97
  
 
98
        vbox2 = gtk.VBox(False, 0)
 
99
        hbox.pack_start(vbox2, True, True, 5)
 
100
  
 
101
        label = gtk.Label("Value :")
 
102
        label.set_alignment(0, 0.5)
 
103
        vbox2.pack_start(label, False, True, 0)
 
104
  
 
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)
 
110
  
 
111
        vbox2 = gtk.VBox(False, 0)
 
112
        hbox.pack_start(vbox2, True, True, 5)
 
113
  
 
114
        label = gtk.Label("Digits :")
 
115
        label.set_alignment(0, 0.5)
 
116
        vbox2.pack_start(label, False, True, 0)
 
117
  
 
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)
 
123
  
 
124
        hbox = gtk.HBox(False, 0)
 
125
        vbox.pack_start(hbox, False, True, 5)
 
126
 
 
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)
 
131
  
 
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)
 
136
  
 
137
        val_label = gtk.Label("")
 
138
  
 
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,
 
143
                       val_label)
 
144
        hbox.pack_start(button, True, True, 5)
 
145
  
 
146
        button = gtk.Button("Value as Float")
 
147
        button.connect("clicked", self.get_value, 2, spinner1, spinner2,
 
148
                       val_label)
 
149
        hbox.pack_start(button, True, True, 5)
 
150
  
 
151
        vbox.pack_start(val_label, True, True, 0)
 
152
        val_label.set_text("0")
 
153
  
 
154
        hbox = gtk.HBox(False, 0)
 
155
        main_vbox.pack_start(hbox, False, True, 0)
 
156
  
 
157
        button = gtk.Button("Close")
 
158
        button.connect("clicked", lambda w: gtk.main_quit())
 
159
        hbox.pack_start(button, True, True, 5)
 
160
        window.show_all()
 
161
 
 
162
def main():
 
163
    gtk.main()
 
164
    return 0
 
165
 
 
166
if __name__ == "__main__":
 
167
    SpinButtonExample()
 
168
    main()