-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTkinter Examples.py
More file actions
419 lines (246 loc) · 11.1 KB
/
Tkinter Examples.py
File metadata and controls
419 lines (246 loc) · 11.1 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
'''TKINTER:'''
'''
Welcome to tkinter, the canvas part of Python. With tkinter you can draw lines and \
shapes such as ovals, arcs and rectangles. You can also create some wild digital \
string-art designs with for-loops. With tkinter you can also create buttons and input \
boxes. We will get into all this and more about tkinter, the fun part of Python programming.
'''
# Let's create a simple tkinter window. Type and execute/run this tkinter program
# example below and see what happens.
from tkinter import*
root=Tk()
'''----------------------------------------------------------------'''
# This simple tkinter program will create an empty window, which is 500 X 500 pixels.
# Type and execute/run this tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500)
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# So far the tinkter window is empty; no canvas colours or anything, but a simple
# grayed out, empty tkinter window. Now, let's add colour to our empty tkinter window
# to sort of see where we are going with tkinter. Type and execute/run the tkinter
# program example below and see what happens when we add the colour black to our
# empty tkinter window.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Now that we have our empty tkinter window, which is now a black, empty tkinter
# window. Let's add a simple, blue diagonal line drawing inside our black, empty
# tkinter window and see what happens when you execute/run the tkinter program
# example below.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_line(0,0,500,500,fill='blue')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Note: hexadecimal values can also be used with the tkinter canvas colour as well as
# the tkinter graphics colour scheme alike. All hexadecimal values in Python start with
# the '#' number sign, then preceding with a six digit hexadecimal number to the right,
# for example '#000000' = black, '#ffffff' = white. See below, a basic hexadecimal RGB
# colour codes list as follows:
# Black = '#000000'
# White = '#ffffff'
# Red = '#ff0000'
# Green = '#00ff00'
# Blue = '#000fff'
# Yellow = '#fff000'
# Pink = '#ff00ff'
# Cyan = '#00ffff'
# Now let's add another diagonal line in the tkinter window and colour it red and
# change the background colour to the hexadecimal colour code, black. type and
# execute/run the tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='#000000')
draw.create_line(0,0,500,500,fill='blue')
draw.create_line(0,500,500,0,fill='red')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's now draw a complete yellow square right in the middle of our X-shaped lines
# and see what happens when you execute/run the tkinter program example below.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_line(0,0,500,500,fill='blue')
draw.create_line(0,500,500,0,fill='red')
draw.create_line(50,50,450,50,450,50,450,450,50,450,50,50,fill='yellow')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Now let's make all the lines in our tkinter drawing thicker. Type and execute/run the
# tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_line(0,0,500,500,fill='blue',width=5)
draw.create_line(0,500,500,0,fill='red',width=5)
draw.create_line(50,50,450,50,450,50,450,450,50,450,50,50,fill='yellow',width=5)
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's draw a simple rectangle with tkinter's 'rectangle' command. Type and
# execute/run the tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_rectangle(150,100,340,400,outline='cyan',width=5)
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's fill the inside of the rectangle with the colour red. Type and execute/run the
# tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_rectangle(150,100,340,400,fill='red',outline='cyan',width=5)
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's draw a simple oval with tkinter's 'oval' command. Type and execute/run the
# tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_oval(150,100,340,400,fill='red',outline='cyan',width=5)
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's draw a simple arc with tkinter's 'arc' command. Type and execute/run the
# tkinter program below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
draw.create_arc(120 ,120,400,400,extent=180,fill='red',outline='cyan',width=5)
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Now let's create a tkinter digital string-art design using a for-loop. Type and
# execute/run the tkinter program example below and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
for i in range(0,400,3):
draw.create_line(50+i,50+i,450,50,450,50,450,450,50,450,50+i,50+i,fill='cyan')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Now let's create a tkinter digital string-art design using tkinter's 'rectangle'
# command with a for-loop. Type and execute/run the tkinter program example below
# and see what happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
for i in range(0,96,5):
draw.create_rectangle(150+i,100+i,340-i,400-i,outline='cyan')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Now let's create a tkinter digital string-art design using tkinter's 'oval' command with
# a for-loop. Type and execute/run the tkinter program example below and see what
# happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
for i in range(0,96,5):
draw.create_oval(150+i,100+i,340-i,400-i,outline='cyan')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Now let's create a tkinter digital string-art design using tkinter's 'arc' command with
# a for-loop. Type and execute/run the tkinter program example below and see what
# happens.
from tkinter import*
root=Tk()
draw=Canvas(root,height=500,width=500,bg='black')
for i in range(0,140,5):
draw.create_arc(120+i ,120+i,400-i,400-i,extent=180,outline='cyan')
draw.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's import an image from your computer with tkinter. See what happens when you
# type and execute/run the tkinter program example below.
from tkinter import*
root=Tk()
photo=PhotoImage(file='C:\\Users\\JCR\\Documents\\Pictures\\image.jpg')
label=Label(root,image=photo)
label.pack()
root.mainloop()
'''----------------------------------------------------------------'''
# Let's set the canvas width and the canvas height, then import an image from your
# computer with tkinter. See what happens when you type and execute/run the tkinter
# program example below.
from tkinter import*
root=Tk()
canvas=Canvas(width=600,height=600,bg='blue')
canvas.pack()
photo=PhotoImage(file='C:\\Users\\JCR\\Documents\\Pictures\\image.jpg')
canvas.create_image(300,300,image=photo)
root.mainloop()
'''----------------------------------------------------------------'''
# Let's add anchoring to an image and position it in the center of the canvas. The
# anchor emitter has up to nine positional value settings: CENTER, N, S, E, W, NW, NE,
# SW, SE. See what happens when you type and execute/run the tkinter program
# example below.
from tkinter import*
root=Tk()
canvas=Canvas(width=600,height=600,bg='blue')
canvas.pack()
photo=PhotoImage(file='C:\\Users\\JCR\\Documents\\Pictures\\image.jpg')
canvas.create_image(300,300,image=photo,anchor=CENTER)
root.mainloop()
'''----------------------------------------------------------------'''
# Let's create a button with tkinter and see what happens when you type and
# execute/run the tkinter program example below.
from tkinter import*
button=Tk()
button.geometry('500x500')
button_1=Button(button,text='Click Me!')
button_1.pack()
button.mainloop()
'''----------------------------------------------------------------'''
# Let's create a label for our tkinter button and see what happens when you type and
# execute/run the tkinter program example below.
from tkinter import*
button=Tk()
button.geometry('500x500')
label_1=Label(button,text=' "Python Programmer\'s Glossary Bible" ')
button_1=Button(button,text='Click Me!')
label_1.pack()
button_1.pack()
button.mainloop()
'''----------------------------------------------------------------'''
# Let's make the tkinter button call the label with a 'def' function. Every time the 'Click
# Me!' button is clicked, the 'def' function gets called and the label is desplayed again.
# See what happens when you type and execute/run the tkinter program example
# below.
from tkinter import*
button=Tk()
def call_the_def_function():
label_1=Label(button,text=' "Python Programmer\'s Glossary Bible" ')
label_1.pack()
button.geometry('500x500')
button_1=Button(button,text='Click Me!',command=call_the_def_function)
button_1.pack()
button.mainloop()
'''----------------------------------------------------------------'''
# Let's reposition the tkinter button and its label by invoking the 'side=TOP' statement
# for the label and the 'side=LEFT' statement for the tkinter button. See what happens
# when you type and execute/run the tkinter program example below.
from tkinter import*
button=Tk()
def call_the_def_function():
label_1=Label(button,text=' "Python Programmer\'s Glossary Bible" ')
label_1.pack(side=TOP)
button.geometry('500x500')
button_1=Button(button,text='Click Me!',command=call_the_def_function)
button_1.pack(side=LEFT)
button.mainloop()
# More Tkinter examples still to come, as I learn more and more, each and every day.