Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ThinkPython/code/widget_demo.py at master · optionalg/ThinkPython · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
optionalg
ThinkPython
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ThinkPython
/
code
/
widget_demo.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
186 lines (124 loc) · 3.58 KB
master
Breadcrumbs
ThinkPython
/
code
/
widget_demo.py
Copy path
Top
File metadata and controls
Code
Blame
186 lines (124 loc) · 3.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""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.
This program demonstrates how to use the Gui module
to create and operate on Tkinter widgets.
The documentation for the widgets is at
http://www.pythonware.com/library/tkinter/introduction/
"""
from
swampy
.
Gui
import
*
# create the Gui: the debug flag makes the frames visible
g
=
Gui
(
debug
=
False
)
# the topmost structure is a row of widgets
g
.
row
()
# FRAME 1
# the first frame is a column of widgets
g
.
col
()
# la is for label
la1
=
g
.
la
(
text
=
'This is a label.'
)
# en is for entry
en
=
g
.
en
()
en
.
insert
(
END
,
'This is an entry widget.'
)
la2
=
g
.
la
(
text
=
''
)
def
press_me
():
"""this callback gets invoked when the user presses the button"""
text
=
en
.
get
()
la2
.
configure
(
text
=
text
)
# bu is for button
bu
=
g
.
bu
(
text
=
'Press me'
,
command
=
press_me
)
# end of the first frame
g
.
endcol
()
# FRAME 2
g
.
col
()
# ca is for canvas
ca
=
g
.
ca
(
width
=
200
,
height
=
200
)
item1
=
ca
.
circle
([
0
,
0
],
70
,
'red'
)
item2
=
ca
.
rectangle
([[
0
,
0
], [
60
,
60
]],
'blue'
)
item3
=
ca
.
text
([
0
,
0
],
'This is a canvas.'
,
'white'
)
# mb is for menubutton
mb
=
g
.
mb
(
text
=
'Choose a color'
)
def
set_color
(
color
):
ca
.
itemconfig
(
item2
,
fill
=
color
)
# mi is for menuitem
for
color
in
[
'red'
,
'green'
,
'blue'
]:
# Callable is an object that can be used like a function
g
.
mi
(
mb
,
color
,
command
=
Callable
(
set_color
,
color
))
g
.
endcol
()
# FRAME 3
g
.
col
()
def
get_selection
():
t
=
lb
.
curselection
()
try
:
index
=
int
(
t
[
0
])
color
=
lb
.
get
(
index
)
return
color
except
:
return
None
def
print_selection
(
event
):
print
get_selection
()
def
apply_color
():
color
=
get_selection
()
if
color
:
ca
.
itemconfig
(
item1
,
fill
=
color
)
la
=
g
.
la
(
text
=
'List of colors:'
)
g
.
row
()
# lb is for listbox
lb
=
g
.
lb
()
lb
.
bind
(
'<ButtonRelease-1>'
,
print_selection
)
# sb is for scrollbar
sb
=
g
.
sb
()
g
.
endrow
()
bu
=
g
.
bu
(
text
=
'Apply color'
,
command
=
apply_color
)
g
.
endcol
()
# fill the listbox with color names
fp
=
open
(
'/etc/X11/rgb.txt'
)
fp
.
readline
()
for
line
in
fp
:
t
=
line
.
split
(
'
\t
'
)
name
=
t
[
2
].
strip
()
lb
.
insert
(
END
,
name
)
# tell the listbox and the scrollbar about each other
lb
.
configure
(
yscrollcommand
=
sb
.
set
)
sb
.
configure
(
command
=
lb
.
yview
)
# FRAME 4
g
.
col
()
# te is for text entry
te
=
g
.
te
(
height
=
5
,
width
=
40
)
te
.
insert
(
END
,
"This is a Text widget.
\n
"
)
te
.
insert
(
END
,
"It's like a little text editor.
\n
"
)
te
.
insert
(
END
,
"It has more than one line, unlike an Entry widget.
\n
"
)
# st is for scrollable text
st
=
g
.
st
()
st
.
text
.
configure
(
height
=
5
,
width
=
40
)
st
.
text
.
insert
(
END
,
"This is a Scrollable Text widget.
\n
"
)
st
.
text
.
insert
(
END
,
"It is defined in Gui.py
\n
"
)
for
i
in
range
(
100
):
st
.
text
.
insert
(
END
,
"All work and no play.
\n
"
)
g
.
endcol
()
# FRAME 5
# gr is for grid: start a grid with three columns
# the rweights control how extra space is divided among the rows
g
.
gr
(
3
,
rweights
=
[
1
,
1
,
1
])
for
i
in
range
(
1
,
10
):
g
.
bu
(
text
=
str
(
i
))
g
.
endgr
()
# FRAME 6
g
.
col
()
def
print_var
(
obj
):
print
obj
.
var
.
get
()
g
.
la
(
text
=
'Font:'
)
fontsize
=
IntVar
()
# rb is for radiobutton
for
size
in
[
10
,
12
,
14
,
16
,
18
]:
rb
=
g
.
rb
(
text
=
str
(
size
),
variable
=
fontsize
,
value
=
size
)
rb
.
configure
(
command
=
Callable
(
print_var
,
rb
))
# cb is for checkbutton
b1
=
g
.
cb
(
text
=
'Bold'
)
b1
.
configure
(
command
=
Callable
(
print_var
,
b1
))
b2
=
g
.
cb
(
text
=
'Italic'
)
b2
.
configure
(
command
=
Callable
(
print_var
,
b2
))
g
.
endcol
()
g
.
mainloop
()
You can’t perform that action at this time.