-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS011L097TkinterChallengeOwnSolutionLoopExperimentAcer.py
More file actions
351 lines (327 loc) · 10.5 KB
/
S011L097TkinterChallengeOwnSolutionLoopExperimentAcer.py
File metadata and controls
351 lines (327 loc) · 10.5 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
#
# TIM BUCHALKA'S COMPLETE PYTHON MASTERCLASS Created by Tim Buchalka, Jean-Paul Roberts,
# Tim Buchalka's Learn Programming Academy.
# This is a collection of my codes, generated by myself for learning Python
# This challenge is about understanding tkinter and making GUI in Python.
# Here is a link to the course: https://www.udemy.com/python-the-complete-python-developer-course/
# Authors of the challenge (definition of what to code)
#
# Tim Buchalka
# https://www.udemy.com/user/timbuchalka/
# https://github.com/tim-buchalka
#
# Jean-Paul Roberts
# https://www.udemy.com/user/jeanpaulroberts/
#
# START OF CHALLENGE (TIM BUCHALKA, JEAN-PAUL ROBERTS)
#
# Write a GUI program to create a simple calculator
# layout that looks like the screenshot.
#
# Try to be as Pythonic as possible - it's ok if you
# end up writing repeated Button and Grid statements,
# but consider using lists and a for loop.
#
# There is no need to store the buttons in variables.
#
# As an optional extra, refer to the documentation to
# work out how to use minsize() to prevent your window
# from being shrunk so that the widgets vanish from view.
#
# Hint: You may want to use the widgets .winfo_height() and
# winfo_width() methods, in which case you should know that
# they will not return the correct results unless the window
# has been forced to draw the widgets by calling its .update()
# method first.
#
# If you are using Windows you will probably find that the
# width is already constrained and can't be resized too small.
# The height will still need to be constrained, though.
#
# END OF THE CHALLENGE (TIM BUCHALKA, JEAN-PAUL ROBERTS)
#
# Python 3.7.0
# IntelliJ IDEA 2019.1.3 (Community Edition)
# Windows 10, 64 bit
#
# START OF MY OWN CODE
#
# # test 1 - experimenting with loops
# for i in range(1, 4):
# print(i)
#
# for i in range(1, 4):
# for j in range(1, 4):
# print(i, j)
# test 2 - generating buttons with loop in tkinter (with grid)
# only in the first row
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 2")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# for i in range (1, 6):
# button = tkinter.Button(mainWindow, text ="{}".format(i))
# button.grid(row=0, column=i, sticky='new')
#
# mainWindow.mainloop()
# test 3 - generating buttons with loop in tkinter (with grid)
# only in the first row
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 3")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# for i in range (1, 6):
# for j in range(1,6):
# button = tkinter.Button(mainWindow, text ="{}, {}".format(i, j))
# button.grid(row=i, column=j, sticky='new')
#
# mainWindow.mainloop()
# test 4 - generating buttons with loop in tkinter (with grid)
# only in the first row
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 4")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# buttonList = ["A", "B", "C", "D", "E"]
#
# for i in range (0, 5):
# button = tkinter.Button(mainWindow, text ="{}".format(buttonList[i]))
# button.grid(row=0, column=i, sticky='new')
#
# mainWindow.mainloop()
# test 5 - generating buttons with loop in tkinter (with grid)
# multiple rows and columns
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 5")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# buttonRowList = ["R1", "R2", "R3", "R4", "R5"]
# buttonColumnList = ["C1", "C2", "C3", "C4", "C5"]
#
#
# for i in range(0, 5):
# for j in range(0, 5):
# button = tkinter.Button(mainWindow, text ="{}, {}".format(buttonRowList[i], buttonColumnList[j]))
# button.grid(row=i, column=j, sticky='new')
#
# mainWindow.mainloop()
# test 6 - generating buttons with loop in tkinter (with grid)
# multiple rows and columns with range for more flexibility
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 6")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# buttonRowList = ["R1", "R2", "R3", "R4", "R5", "R6"]
# buttonColumnList = ["C1", "C2", "C3"]
#
# print(len(buttonRowList))
# print(len(buttonColumnList))
#
# for i in range(0, len(buttonRowList)):
# for j in range(0, len(buttonColumnList)):
# button = tkinter.Button(mainWindow, text ="{}, {}".format(buttonRowList[i], buttonColumnList[j]))
# button.grid(row=i, column=j, sticky='new')
#
# mainWindow.mainloop()
# test 7 - generating buttons with loop in tkinter (with grid)
# multiple rows and columns with range for more flexibility
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 7")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# buttonRowList = ["R1", "R2", "R3", "R4", "R5", "R6"]
# buttonColumnList = ["C1", "C2", "C3"]
#
# print(len(buttonRowList))
# print(len(buttonColumnList))
#
# for i in range(0, len(buttonRowList)):
# for j in range(0, len(buttonColumnList)):
# button = tkinter.Button(mainWindow, text ="{}, {}".format(buttonRowList[i], buttonColumnList[j]))
# button.grid(row=i, column=j, sticky='new')
#
# mainWindow.mainloop()
# test 8 - generating buttons with loop in tkinter (with grid)
# multiple rows and columns with range for more flexibility
#
# buttonList = [["C", "CE"], ["7", "8", "9", "+"], ["4", "5", "6", "-"], ["1", "2", "3", "*"], ["0", "=", "/"]]
# for i in buttonList:
# print(i)
# print("")
#
# for i in buttonList:
# print(*i)
# test 9 - generating buttons with loop in tkinter (with grid)
# multiple rows and columns with range for more flexibility
#
# buttonList = [["C", "CE"], ["7", "8", "9", "+"], ["4", "5", "6", "-"], ["1", "2", "3", "*"], ["0", "=", "/"]]
# for list in buttonList:
# for number in list:
# print(number)
# test 10 - generating buttons with loop in tkinter (with grid)
# figuring out how to retrieve xth sub list, and from it another element
#
# buttonList = [["C", "CE"], ["7", "8", "9", "+"], ["4", "5", "6", "-"], ["1", "2", "3", "*"], ["0", "=", "/"]]
# for i in buttonList:
# print(i)
#
# print("")
# print(buttonList[1])
# print(buttonList[3])
# print(buttonList[3][2])
# test 11 - generating buttons with loop in tkinter (with grid)
# figuring out how to retrieve xth sub list, and from it another element
#
# buttonList = [["C", "CE"], ["7", "8", "9", "+"], ["4", "5", "6", "-"], ["1", "2", "3", "*"], ["0", "=", "/"]]
# for i in buttonList:
# print(i)
#
# print("")
# print(buttonList[1])
# print(buttonList[3])
# print(buttonList[3][2])
#
# print("")
# print("")
# print(len(buttonList))
# print("")
# for i in range(0, len(buttonList)):
# print(i)
#
# for i in range(0, len(buttonList)):
# for j in range (0, len(buttonList[i])):
# print(i, j)
# test 12 - generating buttons with loop in tkinter (with grid)
# generating buttons based on a list - the "pythonic" solution 1.0
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 12")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# buttonList = [["C", "CE"],
# ["7", "8", "9", "+"],
# ["4", "5", "6", "-"],
# ["1", "2", "3", "*"],
# ["0", "=", "/"]]
#
# for i in range(0, len(buttonList)):
# for j in range (0, len(buttonList[i])):
# print(i, j)
#
# for i in range(0, len(buttonList)):
# for j in range(0, len(buttonList[i])):
# button = tkinter.Button(mainWindow, text ="{}".format(buttonList[i][j]))
# button.grid(row=i+1, column=j+1, sticky='new')
#
# mainWindow.mainloop()
# test 13 - generating buttons with loop in tkinter (with grid)
# generating buttons based on a list - the "pythonic" solution 2.0
#
#
# try:
# import tkinter
# except ImportError: # Python 2
# import Tkinter as tkinter
#
# mainWindow = tkinter.Tk()
#
# mainWindow.title("tkinter button loop test 13")
# mainWindow.geometry('250x250+8+400')
# mainWindow['padx'] = 10
#
# buttonList = [["C", "CE"],
# ["7", "8", "9", "+"],
# ["4", "5", "6", "-"],
# ["1", "2", "3", "*"],
# ["0", "=", "/"]]
#
# columnSpanList = [[1, 1],
# [1, 1, 1, 1],
# [1, 1, 1, 1],
# [1, 1, 1, 1],
# [1, 1, 1]]
#
# for i in range(0, len(buttonList)):
# for j in range(0, len(buttonList[i])):
# button = tkinter.Button(mainWindow, text="{}".format(buttonList[i][j]))
# button.grid(row=i+1, column=j+1, sticky='new', columnspan=columnSpanList[i][j])
#
# mainWindow.mainloop()
# test 14 - generating buttons with loop in tkinter (with grid)
# generating buttons based on a list - the "pythonic" solution 3.0
# generating automatic column span adaption
#
try:
import tkinter
except ImportError: # Python 2
import Tkinter as tkinter
mainWindow = tkinter.Tk()
mainWindow.title("tkinter button loop test 14")
mainWindow.geometry('250x250+8+400')
mainWindow['padx'] = 10
buttonList = [["C", "CE"],
["7", "8", "9", "+"],
["4", "5", "6", "-"],
["1", "2", "3", "*"],
["0", "=", "/"]]
columnSpanList = [[1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 2, 1]]
for i in range(0, len(buttonList)):
for j in range(0, len(buttonList[i])):
button = tkinter.Button(mainWindow, text="{}".format(buttonList[i][j]))
button.grid(row=i+1, column=j+columnSpanList[i][j-1], sticky='new', columnspan=columnSpanList[i][j]) # adapting
mainWindow.mainloop()
# Further steps
# double space for certain butten
# min width and height