File: packbox.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 (254 lines) | stat: -rw-r--r-- 9,464 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python

# example packbox.py

import pygtk
pygtk.require('2.0')
import gtk
import sys, string

# Helper function that makes a new hbox filled with button-labels. Arguments
# for the variables we're interested are passed in to this function.  We do
# not show the box, but do show everything inside.

def make_box(homogeneous, spacing, expand, fill, padding):

    # Create a new hbox with the appropriate homogeneous
    # and spacing settings
    box = gtk.HBox(homogeneous, spacing)

    # Create a series of buttons with the appropriate settings
    button = gtk.Button("box.pack")
    box.pack_start(button, expand, fill, padding)
    button.show()

    button = gtk.Button("(button,")
    box.pack_start(button, expand, fill, padding)
    button.show()

    # Create a button with the label depending on the value of
    # expand.
    if expand == gtk.TRUE:
        button = gtk.Button("TRUE,")
    else:
        button = gtk.Button("FALSE,")

    box.pack_start(button, expand, fill, padding)
    button.show()

    # This is the same as the button creation for "expand"
    # above, but uses the shorthand form.
    button = gtk.Button(("FALSE,", "TRUE,")[fill==gtk.TRUE])
    box.pack_start(button, expand, fill, padding)
    button.show()

    padstr = "%d)" % padding

    button = gtk.Button(padstr)
    box.pack_start(button, expand, fill, padding)
    button.show()
    return box

class PackBox1:
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return gtk.FALSE

    def __init__(self, which):

        # Create our window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        # You should always remember to connect the delete_event signal
        # to the main window. This is very important for proper intuitive
        # behavior
        self.window.connect("delete_event", self.delete_event)
        self.window.set_border_width(10)
    
        # We create a vertical box (vbox) to pack the horizontal boxes into.
        # This allows us to stack the horizontal boxes filled with buttons one
        # on top of the other in this vbox.
        box1 = gtk.VBox(gtk.FALSE, 0)
    
        # which example to show. These correspond to the pictures above.
        if which == 1:
            # create a new label.
            label = gtk.Label("HBox(FALSE, 0)")
	
            # Align the label to the left side.  We'll discuss this method
            # and others in the section on Widget Attributes.
            label.set_alignment(0, 0)

            # Pack the label into the vertical box (vbox box1).  Remember that 
            # widgets added to a vbox will be packed one on top of the other in
            # order.
            box1.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
	
            # Show the label
            label.show()
	
            # Call our make box function - homogeneous = FALSE, spacing = 0,
            # expand = FALSE, fill = FALSE, padding = 0
            box2 = make_box(gtk.FALSE, 0, gtk.FALSE, gtk.FALSE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()

            # Call our make box function - homogeneous = FALSE, spacing = 0,
            # expand = TRUE, fill = FALSE, padding = 0
            box2 = make_box(gtk.FALSE, 0, gtk.TRUE, gtk.FALSE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.FALSE, 0, gtk.TRUE, gtk.TRUE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # Creates a separator, we'll learn more about these later, 
            # but they are quite simple.
            separator = gtk.HSeparator()
	
            # Pack the separator into the vbox. Remember each of these
            # widgets is being packed into a vbox, so they'll be stacked
            # vertically.
            box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 5)
            separator.show()
	
            # Create another new label, and show it.
            label = gtk.Label("HBox(TRUE, 0)")
            label.set_alignment(0, 0)
            box1.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
            label.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.TRUE, 0, gtk.TRUE, gtk.FALSE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.TRUE, 0, gtk.TRUE, gtk.TRUE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # Another new separator.
            separator = gtk.HSeparator()
            # The last 3 arguments to pack_start are:
            # expand, fill, padding.
            box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 5)
            separator.show()
        elif which == 2:
            # Create a new label, remember box1 is a vbox as created 
            # near the beginning of __init__()
            label = gtk.Label("HBox(FALSE, 10)")
            label.set_alignment( 0, 0)
            box1.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
            label.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.FALSE, 10, gtk.TRUE, gtk.FALSE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.FALSE, 10, gtk.TRUE, gtk.TRUE, 0)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            separator = gtk.HSeparator()
            # The last 3 arguments to pack_start are:
            # expand, fill, padding.
            box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 5)
            separator.show()
	
            label = gtk.Label("HBox(FALSE, 0)")
            label.set_alignment(0, 0)
            box1.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
            label.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.FALSE, 0, gtk.TRUE, gtk.FALSE, 10)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # Args are: homogeneous, spacing, expand, fill, padding
            box2 = make_box(gtk.FALSE, 0, gtk.TRUE, gtk.TRUE, 10)
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            separator = gtk.HSeparator()
            # The last 3 arguments to pack_start are:
            # expand, fill, padding.
            box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 5)
            separator.show()

        elif which == 3:

            # This demonstrates the ability to use pack_end() to
            # right justify widgets. First, we create a new box as before.
            box2 = make_box(gtk.FALSE, 0, gtk.FALSE, gtk.FALSE, 0)

            # Create the label that will be put at the end.
            label = gtk.Label("end")
            # Pack it using pack_end(), so it is put on the right
            # side of the hbox created in the make_box() call.
            box2.pack_end(label, gtk.FALSE, gtk.FALSE, 0)
            # Show the label.
            label.show()
	
            # Pack box2 into box1
            box1.pack_start(box2, gtk.FALSE, gtk.FALSE, 0)
            box2.show()
	
            # A separator for the bottom.
            separator = gtk.HSeparator()
            
            # This explicitly sets the separator to 400 pixels wide by 5
            # pixels high. This is so the hbox we created will also be 400
            # pixels wide, and the "end" label will be separated from the
            # other labels in the hbox. Otherwise, all the widgets in the
            # hbox would be packed as close together as possible.
            separator.set_size_request(400, 5)
            # pack the separator into the vbox (box1) created near the start 
            # of __init__()
            box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 5)
            separator.show()
    
        # Create another new hbox.. remember we can use as many as we need!
        quitbox = gtk.HBox(gtk.FALSE, 0)
    
        # Our quit button.
        button = gtk.Button("Quit")
    
        # Setup the signal to terminate the program when the button is clicked
        button.connect("clicked", lambda w: gtk.main_quit())
        # Pack the button into the quitbox.
        # The last 3 arguments to pack_start are:
        # expand, fill, padding.
        quitbox.pack_start(button, gtk.TRUE, gtk.FALSE, 0)
        # pack the quitbox into the vbox (box1)
        box1.pack_start(quitbox, gtk.FALSE, gtk.FALSE, 0)
    
        # Pack the vbox (box1) which now contains all our widgets, into the
        # main window.
        self.window.add(box1)
    
        # And show everything left
        button.show()
        quitbox.show()
    
        box1.show()
        # Showing the window last so everything pops up at once.
        self.window.show()

def main():
    # And of course, our main loop.
    gtk.main()
    # Control returns here when main_quit() is called
    return 0         

if __name__ =="__main__":
    if len(sys.argv) != 2:
        sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
        sys.exit(1)
    PackBox1(string.atoi(sys.argv[1]))
    main()