forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack_demo.py
176 lines (116 loc) · 3.21 KB
/
pack_demo.py
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
"""Solution to an exercise from
Think Python: An Introduction to Software Design
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
This program requires Gui.py, which is part of
Swampy; you can download it from thinkpython.com/swampy.
"""
from swampy.Gui import *
def figure1():
print 'Figure 17.3 a'
g = Gui()
b1 = g.bu(text='OK', command=g.quit)
b2 = g.bu(text='Cancel')
b3 = g.bu(text='Help')
g.mainloop()
def figure2():
print 'Figure 17.3 b'
b2.configure(text='Cancel Command')
g.mainloop()
def figure3():
print 'Figure 17.3 c'
for b in [b1, b2, b3]:
b.pack(side=TOP)
g.mainloop()
g.destroy()
def figure4():
print 'Figure 17.4 a'
g = Gui()
options = dict(side=LEFT, padx=10, pady=10)
b1 = g.bu(text='OK', command=g.quit, **options)
b2 = g.bu(text='Cancel', **options)
b3 = g.bu(text='Help', **options)
g.mainloop()
def figure5():
print 'Figure 17.4 b'
options = dict(side=LEFT, padx=0, pady=0, ipadx=10, ipady=10)
for b in [b1, b2, b3]:
b.pack(**options)
g.mainloop()
def figure6():
print 'Figure 17.4 c'
options = dict(side=LEFT, padx=10, pady=10, ipadx=10, ipady=10)
for b in [b1, b2, b3]:
b.pack(**options)
g.mainloop()
g.destroy()
def figure7():
print 'Figure 17.5'
g = Gui()
options = dict(side=TOP, fill=X)
b1 = g.bu(text='OK', command=g.quit, **options)
b2 = g.bu(text='Cancel Command', **options)
b3 = g.bu(text='Help', **options)
g.mainloop()
g.destroy()
def figure8():
print 'Figure 17.6'
g = Gui()
options = dict(side=TOP, fill=X)
# create the widgets
g.fr()
la = g.la(side=TOP, text='List of colors:')
lb = g.lb(side=LEFT)
sb = g.sb(side=RIGHT, fill=Y)
g.endfr()
bu = g.bu(side=BOTTOM, text='OK', command=g.quit)
# fill the listbox with color names
colors = []
for line in open('/etc/X11/rgb.txt'):
t = line.split('\t')
name = t[-1].strip()
colors.append(name)
for color in colors:
lb.insert(END, color)
# tell the listbox and the scrollbar about each other
lb.configure(yscrollcommand=sb.set)
sb.configure(command=lb.yview)
g.mainloop()
g.destroy()
def figure9():
print 'Figure 17.7 a'
g = Gui()
options = dict(side=LEFT)
b1 = g.bu(text='OK', command=g.quit, **options)
b2 = g.bu(text='Cancel', **options)
b3 = g.bu(text='Help', **options)
g.geometry('300x150')
g.mainloop()
def figure10():
print 'Figure 17.7 b'
def pack(**options):
for b in [b1, b2, b3]:
b.pack(**options)
pack(side=LEFT, fill=X, expand=1)
g.mainloop()
def figure11():
print 'Figure 17.7 c'
options = dict(side=LEFT, fill=NONE, expand=0)
b1.pack(**options)
b2.pack(**options)
# the override function is defined in Gui.py
override(options, expand=1)
b3.pack(**options)
g.mainloop()
def figure12():
print 'Figure 17.7 d'
pack(**options)
g.mainloop()
def figure13():
print 'Figure 17.7 e'
options = dict(side=LEFT, fill=BOTH, expand=1)
pack(**options)
g.mainloop()
def main():
figure8()
main()