tkinter graphics

A silly demo of tkinter

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()

Other tkinter demos

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

The Python script:
draw the flag pixel by pixel

See Developing tkinter applications.

tkflag.py

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
00
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
3131
3232
3333
3434
3535
3636
3737
3838
3939
400
411
422
433
444
455
466
477
488
499
5010
5111
5212
5313
5414
5515
5616
5717
5818
5919
6020
6121
6222
6323
6424
6525
6626
6727
6828
6929
7030
7131
7232
7333
7434
7535
7636
7737
7838
7939
800
811
822
833
844
855
866
877
888
899
9010
9111
9212
9313
9414
9515
9616
9717
9818
9919
10020
10121
10222
10323
10424
10525
10626
10727
10828
10929
11030
11131
11232
11333
11434
11535
11636
11737
11838
11939
1200
1211
1222
1233
1244
1255
1266
1277
1288
1299
13010
13111
13212
13313
13414
13515
13616
13717
13818
13919
14020
14121
14222
14323
14424
14525
14626
14727
14828
14929
15030
15131
15232
15333
15434
15535
15636
15737
15838
15939
1600
1611
1622
1633
1644
1655
1666
1677
1688
1699
17010
17111
17212
17313
17414
17515
17616
17717
17818
17919
18020
18121
18222
18323
18424
18525
18626
18727
18828
18929
19030
19131
19232
19333
19434
19535
19636
19737
19838
19939
2000
2011
2022
2033
2044
2055
2066
2077
2088
2099
21010
21111
21212
21313
21414
21515
21616
21717
21818
21919
22020
22121
22222
22323
22424
22525
22626
22727
22828
22929
23030
23131
23232
23333
23434
23535
23636
23737
23838
23939
2400
2411
2422
2433
2444
2455
2466
2477
2488
2499
25010
25111
25212
25313
25414
25515
25616
25717
25818
25919

Draw the flag rectangle by rectangle

flag2.py

And give the flag a white background color.

Give the flag one star

flag3.py

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")

Things to try

  1. Wikipedia gives us the red/green/blue numbers (under “8-bit hex”) to fine-tune the colors. To draw a blue pixel,
                    color = "#3C3B6E"   #blue: decimal 60, 59, 110
    
    To draw a white pixel,
                    color = "#FFFFFF"   #white: decimal 255, 255, 255
    
    To 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)
    
  2. Better yet, create three variables immediately before the canvas = statement.
    #red/green/blue colors
    oldGloryRed   = "#B22234"   #decimal  60,  59, 110
    oldGloryWhite = "#FFFFFF"   #decimal 255, 255, 255
    oldGloryBlue  = "#3C3B6E"   #decimal 178,  34,  52
    
    Then use the variables like this:
                    color = oldGloryRed
    
    etc.