On macOS and Microsoft Windows:
python3 -m tkinter
C:\Users\Myname>py.exe -m tkinter
This is Tcl/Tk version 8.6 This should be a cedilla: ç Click me! QUIT
tkinter
,
like the other
modules
that we
import,
is a file.
To find this file,
python3 -c 'import sys; import tkinter; print(sys.modules["tkinter"])' <module 'tkinter' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py'>
C:\Users\Myname>py.exe -c "import sys; import tkinter; print(sys.modules['tkinter'])" <module 'tkinter' from 'C:\\Users\\Myname\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter\\__init.py__'>
Each module can contain some extra demo code
that is usually not executed when we import the module.
For example, the
tkinter
module contains the cedilla demo we just saw.
To execute the demo code,
the
-m
option of
python3
puts the string
"__main__"
(with a total of four underscores)
into the variable
__name__
in the file
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py
,
causing the following
if
statement in this file to be true.
def _test(): root = Tk() text = "This is Tcl/Tk version %s" % TclVersion text += "\nThis should be a cedilla: \xe7" etc. root.mainloop() if __name__ == '__main__': _test()
cd /Library/Frameworks/Python.framework/Versions/3.8/share/doc/python3.8/examples/Tools/demo pwd /Library/Frameworks/Python.framework/Versions/3.8/share/doc/python3.8/examples/Tools/demo ls -l cat README This directory contains a collection of demonstration scripts for various aspects of Python programming. beer.py Well-known programming example: Bottles of beer. eiffel.py Python advanced magic: A metaclass for Eiffel post/preconditions. hanoi.py Well-known programming example: Towers of Hanoi. life.py Curses programming: Simple game-of-life. markov.py Algorithms: Markov chain simulation. mcast.py Network programming: Send and receive UDP multicast packets. queens.py Well-known programming example: N-Queens problem. redemo.py Regular Expressions: GUI script to test regexes. rpython.py Network programming: Small client for remote code execution. rpythond.py Network programming: Small server for remote code execution. sortvisu.py GUI programming: Visualization of different sort algorithms. ss1.py GUI/Application programming: A simple spreadsheet application. vector.py Python basics: A vector class with demonstrating special methods. grep tkinter *.py python3 hanoi.py ./hanoi.py ./sortvisu.py ./ss1.py ./redemo.py
See Developing tkinter applications.
See
Flag
of the United States,
the
Canvas
widget,
the
create_rectangle
function,
and
Capture
a window.
The
y
in
line
23
goes from 0 to 259 inclusive.
These values are in the first column of the following table.
The values of the expression
y % (2 * stripeHeight)
in
line
27
are in the second column.
These values go from 0 to 39 over and over again.
The ones that are less than 20 are red;
the others are white.
(See
lines
27–30.)
Note that there are seven groups of red,
and six groups of white.
y | y % 40 |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
10 | 10 |
11 | 11 |
12 | 12 |
13 | 13 |
14 | 14 |
15 | 15 |
16 | 16 |
17 | 17 |
18 | 18 |
19 | 19 |
20 | 20 |
21 | 21 |
22 | 22 |
23 | 23 |
24 | 24 |
25 | 25 |
26 | 26 |
27 | 27 |
28 | 28 |
29 | 29 |
30 | 30 |
31 | 31 |
32 | 32 |
33 | 33 |
34 | 34 |
35 | 35 |
36 | 36 |
37 | 37 |
38 | 38 |
39 | 39 |
40 | 0 |
41 | 1 |
42 | 2 |
43 | 3 |
44 | 4 |
45 | 5 |
46 | 6 |
47 | 7 |
48 | 8 |
49 | 9 |
50 | 10 |
51 | 11 |
52 | 12 |
53 | 13 |
54 | 14 |
55 | 15 |
56 | 16 |
57 | 17 |
58 | 18 |
59 | 19 |
60 | 20 |
61 | 21 |
62 | 22 |
63 | 23 |
64 | 24 |
65 | 25 |
66 | 26 |
67 | 27 |
68 | 28 |
69 | 29 |
70 | 30 |
71 | 31 |
72 | 32 |
73 | 33 |
74 | 34 |
75 | 35 |
76 | 36 |
77 | 37 |
78 | 38 |
79 | 39 |
80 | 0 |
81 | 1 |
82 | 2 |
83 | 3 |
84 | 4 |
85 | 5 |
86 | 6 |
87 | 7 |
88 | 8 |
89 | 9 |
90 | 10 |
91 | 11 |
92 | 12 |
93 | 13 |
94 | 14 |
95 | 15 |
96 | 16 |
97 | 17 |
98 | 18 |
99 | 19 |
100 | 20 |
101 | 21 |
102 | 22 |
103 | 23 |
104 | 24 |
105 | 25 |
106 | 26 |
107 | 27 |
108 | 28 |
109 | 29 |
110 | 30 |
111 | 31 |
112 | 32 |
113 | 33 |
114 | 34 |
115 | 35 |
116 | 36 |
117 | 37 |
118 | 38 |
119 | 39 |
120 | 0 |
121 | 1 |
122 | 2 |
123 | 3 |
124 | 4 |
125 | 5 |
126 | 6 |
127 | 7 |
128 | 8 |
129 | 9 |
130 | 10 |
131 | 11 |
132 | 12 |
133 | 13 |
134 | 14 |
135 | 15 |
136 | 16 |
137 | 17 |
138 | 18 |
139 | 19 |
140 | 20 |
141 | 21 |
142 | 22 |
143 | 23 |
144 | 24 |
145 | 25 |
146 | 26 |
147 | 27 |
148 | 28 |
149 | 29 |
150 | 30 |
151 | 31 |
152 | 32 |
153 | 33 |
154 | 34 |
155 | 35 |
156 | 36 |
157 | 37 |
158 | 38 |
159 | 39 |
160 | 0 |
161 | 1 |
162 | 2 |
163 | 3 |
164 | 4 |
165 | 5 |
166 | 6 |
167 | 7 |
168 | 8 |
169 | 9 |
170 | 10 |
171 | 11 |
172 | 12 |
173 | 13 |
174 | 14 |
175 | 15 |
176 | 16 |
177 | 17 |
178 | 18 |
179 | 19 |
180 | 20 |
181 | 21 |
182 | 22 |
183 | 23 |
184 | 24 |
185 | 25 |
186 | 26 |
187 | 27 |
188 | 28 |
189 | 29 |
190 | 30 |
191 | 31 |
192 | 32 |
193 | 33 |
194 | 34 |
195 | 35 |
196 | 36 |
197 | 37 |
198 | 38 |
199 | 39 |
200 | 0 |
201 | 1 |
202 | 2 |
203 | 3 |
204 | 4 |
205 | 5 |
206 | 6 |
207 | 7 |
208 | 8 |
209 | 9 |
210 | 10 |
211 | 11 |
212 | 12 |
213 | 13 |
214 | 14 |
215 | 15 |
216 | 16 |
217 | 17 |
218 | 18 |
219 | 19 |
220 | 20 |
221 | 21 |
222 | 22 |
223 | 23 |
224 | 24 |
225 | 25 |
226 | 26 |
227 | 27 |
228 | 28 |
229 | 29 |
230 | 30 |
231 | 31 |
232 | 32 |
233 | 33 |
234 | 34 |
235 | 35 |
236 | 36 |
237 | 37 |
238 | 38 |
239 | 39 |
240 | 0 |
241 | 1 |
242 | 2 |
243 | 3 |
244 | 4 |
245 | 5 |
246 | 6 |
247 | 7 |
248 | 8 |
249 | 9 |
250 | 10 |
251 | 11 |
252 | 12 |
253 | 13 |
254 | 14 |
255 | 15 |
256 | 16 |
257 | 17 |
258 | 18 |
259 | 19 |
And give the flag a white background color.
See the function
create_polygon
.
For space cadets only:
if you’d like to fill in the blue pentagon,
each vertex of the blue pentagon is
radius * (1 – cos(18)tan(36)) / cos(36)
from the center of the blue pentagon,
where the angles are in degrees.
#Another way to pass the 11 arguments to create_polygon. #For the asterisk, see Unpacking Argument Lists. vertices = [] #an empty list for i in range(5): vertices.append(xCenter + radius * math.cos(start + i * delta)) vertices.append(yCenter - radius * math.sin(start + i * delta)) canvas.create_polygon(*vertices, fill = "white")
color = "#3C3B6E" #blue: decimal 60, 59, 110To draw a white pixel,
color = "#FFFFFF" #white: decimal 255, 255, 255To draw a red pixel,
color = "#B22234" #red: decimal 178, 34, 52
Since the
#
is within a
quoted
string,
it is not a
comment
delimiter.
The
#
is followed by three numbers written in hexadecimal.
Each number is written as two hexadecimal digits,
for a total of six hexadecimal digits.
The first number is the amount of red.
The second number is the amount of green.
The third number is the amount of blue.
Each number (written as two hexadecimal digits)
is an integer in the range 0 to 255 inclusive.
For example,
the reddish color
#B22234
has a preponderance of red:
B2
represents the integer 178,
22
represents the integer 34,
and
34
represents the integer 52.
Here’s how to use IDLE to translate the three numbers 178, 34, 52 to and from hexadecimal:
>>> f"#{178:02X}{34:02X}{52:02X}" '#B22234' >>> (0xB2, 0x22, 0x34) (178, 34, 52)
canvas =
statement.
#red/green/blue colors oldGloryRed = "#B22234" #decimal 60, 59, 110 oldGloryWhite = "#FFFFFF" #decimal 255, 255, 255 oldGloryBlue = "#3C3B6E" #decimal 178, 34, 52Then use the variables like this:
color = oldGloryRedetc.