File: rangewidgets.py

package info (click to toggle)
python-gtk2-tutorial 2.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,376 kB
  • ctags: 918
  • sloc: python: 5,731; makefile: 36
file content (236 lines) | stat: -rw-r--r-- 7,738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python

# example rangewidgets.py

import pygtk
pygtk.require('2.0')
import gtk

# Convenience functions

def make_menu_item(name, callback, data=None):
    item = gtk.MenuItem(name)
    item.connect("activate", callback, data)
    item.show()
    return item

def scale_set_default_values(scale):
    scale.set_update_policy(gtk.UPDATE_CONTINUOUS)
    scale.set_digits(1)
    scale.set_value_pos(gtk.POS_TOP)
    scale.set_draw_value(gtk.TRUE)

class RangeWidgets:
    def cb_pos_menu_select(self, item, pos):
        # Set the value position on both scale widgets
        self.hscale.set_value_pos(pos)
        self.vscale.set_value_pos(pos)

    def cb_update_menu_select(self, item, policy):
        # Set the update policy for both scale widgets
        self.hscale.set_update_policy(policy)
        self.vscale.set_update_policy(policy)

    def cb_digits_scale(self, adj):
        # Set the number of decimal places to which adj->value is rounded
        self.hscale.set_digits(adj.value)
        self.vscale.set_digits(adj.value)

    def cb_page_size(self, get, set):
        # Set the page size and page increment size of the sample
        # adjustment to the value specified by the "Page Size" scale
        set.page_size = get.value
        set.page_incr = get.value
        # Now emit the "changed" signal to reconfigure all the widgets that
        # are attached to this adjustment
        set.emit("changed")

    def cb_draw_value(self, button):
        # Turn the value display on the scale widgets off or on depending
        # on the state of the checkbutton
        self.hscale.set_draw_value(button.get_active())
        self.vscale.set_draw_value(button.get_active())  

    # makes the sample window

    def __init__(self):
        # Standard window-creating stuff
        self.window = gtk.Window (gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", lambda w: gtk.main_quit())
        self.window.set_title("range controls")

        box1 = gtk.VBox(gtk.FALSE, 0)
        self.window.add(box1)
        box1.show()

        box2 = gtk.HBox(gtk.FALSE, 10)
        box2.set_border_width(10)
        box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
        box2.show()

        # value, lower, upper, step_increment, page_increment, page_size
        # Note that the page_size value only makes a difference for
        # scrollbar widgets, and the highest value you'll get is actually
        # (upper - page_size).
        adj1 = gtk.Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0)
  
        self.vscale = gtk.VScale(adj1)
        scale_set_default_values(self.vscale)
        box2.pack_start(self.vscale, gtk.TRUE, gtk.TRUE, 0)
        self.vscale.show()

        box3 = gtk.VBox(gtk.FALSE, 10)
        box2.pack_start(box3, gtk.TRUE, gtk.TRUE, 0)
        box3.show()

        # Reuse the same adjustment
        self.hscale = gtk.HScale(adj1)
        self.hscale.set_size_request(200, 30)
        scale_set_default_values(self.hscale)
        box3.pack_start(self.hscale, gtk.TRUE, gtk.TRUE, 0)
        self.hscale.show()

        # Reuse the same adjustment again
        scrollbar = gtk.HScrollbar(adj1)
        # Notice how this causes the scales to always be updated
        # continuously when the scrollbar is moved
        scrollbar.set_update_policy(gtk.UPDATE_CONTINUOUS)
        box3.pack_start(scrollbar, gtk.TRUE, gtk.TRUE, 0)
        scrollbar.show()

        box2 = gtk.HBox(gtk.FALSE, 10)
        box2.set_border_width(10)
        box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
        box2.show()

        # A checkbutton to control whether the value is displayed or not
        button = gtk.CheckButton("Display value on scale widgets")
        button.set_active(gtk.TRUE)
        button.connect("toggled", self.cb_draw_value)
        box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
        button.show()
  
        box2 = gtk.HBox(gtk.FALSE, 10)
        box2.set_border_width(10)

        # An option menu to change the position of the value
        label = gtk.Label("Scale Value Position:")
        box2.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
        label.show()
  
        opt = gtk.OptionMenu()
        menu = gtk.Menu()

        item = make_menu_item ("Top", self.cb_pos_menu_select, gtk.POS_TOP)
        menu.append(item)
  
        item = make_menu_item ("Bottom", self.cb_pos_menu_select,
                               gtk.POS_BOTTOM)
        menu.append(item)
  
        item = make_menu_item ("Left", self.cb_pos_menu_select, gtk.POS_LEFT)
        menu.append(item)
  
        item = make_menu_item ("Right", self.cb_pos_menu_select, gtk.POS_RIGHT)
        menu.append(item)
  
        opt.set_menu(menu)
        box2.pack_start(opt, gtk.TRUE, gtk.TRUE, 0)
        opt.show()

        box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
        box2.show()

        box2 = gtk.HBox(gtk.FALSE, 10)
        box2.set_border_width(10)

        # Yet another option menu, this time for the update policy of the
        # scale widgets
        label = gtk.Label("Scale Update Policy:")
        box2.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
        label.show()
  
        opt = gtk.OptionMenu()
        menu = gtk.Menu()
  
        item = make_menu_item("Continuous", self.cb_update_menu_select,
                              gtk.UPDATE_CONTINUOUS)
        menu.append(item)
  
        item = make_menu_item ("Discontinuous", self.cb_update_menu_select,
                               gtk.UPDATE_DISCONTINUOUS)
        menu.append(item)
  
        item = make_menu_item ("Delayed", self.cb_update_menu_select,
                               gtk.UPDATE_DELAYED)
        menu.append(item)
  
        opt.set_menu(menu)
        box2.pack_start(opt, gtk.TRUE, gtk.TRUE, 0)
        opt.show()
  
        box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
        box2.show()

        box2 = gtk.HBox(gtk.FALSE, 10)
        box2.set_border_width(10)
  
        # An HScale widget for adjusting the number of digits on the
        # sample scales.
        label = gtk.Label("Scale Digits:")
        box2.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
        label.show()

        adj2 = gtk.Adjustment(1.0, 0.0, 5.0, 1.0, 1.0, 0.0)
        adj2.connect("value_changed", self.cb_digits_scale)
        scale = gtk.HScale(adj2)
        scale.set_digits(0)
        box2.pack_start(scale, gtk.TRUE, gtk.TRUE, 0)
        scale.show()

        box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
        box2.show()
  
        box2 = gtk.HBox(gtk.FALSE, 10)
        box2.set_border_width(10)
  
        # And, one last HScale widget for adjusting the page size of the
        # scrollbar.
        label = gtk.Label("Scrollbar Page Size:")
        box2.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
        label.show()

        adj2 = gtk.Adjustment(1.0, 1.0, 101.0, 1.0, 1.0, 0.0)
        adj2.connect("value_changed", self.cb_page_size, adj1)
        scale = gtk.HScale(adj2)
        scale.set_digits(0)
        box2.pack_start(scale, gtk.TRUE, gtk.TRUE, 0)
        scale.show()

        box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
        box2.show()

        separator = gtk.HSeparator()
        box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 0)
        separator.show()

        box2 = gtk.VBox(gtk.FALSE, 10)
        box2.set_border_width(10)
        box1.pack_start(box2, gtk.FALSE, gtk.TRUE, 0)
        box2.show()

        button = gtk.Button("Quit")
        button.connect("clicked", lambda w: gtk.main_quit())
        box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
        button.set_flags(gtk.CAN_DEFAULT)
        button.grab_default()
        button.show()
        self.window.show()

def main():
    gtk.main()
    return 0            

if __name__ == "__main__":
    RangeWidgets()
    main()