SHOW:
|
|
- or go back to the newest paste.
1 | try: | |
2 | from tkinter import * | |
3 | except: | |
4 | from Tkinter import * | |
5 | ||
6 | import random | |
7 | ||
8 | class Application: | |
9 | concat = [] | |
10 | inputList = [] # Gets the input from the keypad and the answer text | |
11 | callIf = [True] # Checks if the countdown is at zero and allows other functions to be called when condition is met | |
12 | bit = {} # Dictonary that houses the bits | |
13 | bitList = [] # List that takes data from the bit dictionary and passes it to the tkinter Labels | |
14 | byteCache = '' # Stores the byte so it can be compared to the answerbyte and no errors are made in the comparison | |
15 | score = 0 # Keeps the score of when the user gets a byte right | |
16 | ||
17 | def __init__(self): | |
18 | ||
19 | master.wm_title("Binary Math") # Sets name of the Window | |
20 | ||
21 | def countdown(self, number): | |
22 | ||
23 | number -= 1 # subtracts 1 from number for countdown | |
24 | ||
25 | self.timeText_n.config(text=number) # Sets text to variable number | |
26 | ||
27 | if number <=0: # Checks If timer is less than or equal to zero | |
28 | self.timeText_n.config(text='Times Up!') # Changes text | |
29 | Application.callIf[0] = True # Changes the callIf to false to not let any other functions be calls and not allow user to do things that are not intended | |
30 | ||
31 | for i in range(0,8): # Sets everything in bitList to 0 | |
32 | self.bitList[i] = 0 | |
33 | ||
34 | Application.setText(self) # Sets all the text to 0 of the binaryBoxes | |
35 | ||
36 | else: | |
37 | master.after(1000, self.countdown, number) # If timer is not less then zero it counts until it is zero | |
38 | Application.callIf[0] = False | |
39 | ||
40 | def randomByte(self): # Function that randomly creates 8 bits and stores them in the dictionary 'bit' which parses it then sends parsed strings into list 'bitList' | |
41 | ||
42 | if Application.callIf[0] == True: | |
43 | ||
44 | try: | |
45 | self.bitList.clear() # Clears anything that may be in the bitList | |
46 | except: | |
47 | self.bitList[:]=[] | |
48 | ||
49 | for i in range(0,8): # Loops that sets dictionary of bit[1-7] then sets bitList[1-7] to the value in each dictionary spot | |
50 | self.bit['bit' + str(i)] = random.randint(0,1) | |
51 | self.bitList.append(str(self.bit['bit' + str(i)])) | |
52 | Application.byteCache = ''.join(self.bitList) # Saves the byte to byteCache to be used later and not cause any unintended errors | |
53 | print(self.bit) | |
54 | print(self.bitList) | |
55 | print(Application.byteCache) | |
56 | ||
57 | def setText(self): # Function sets the text when the start button is clicked | |
58 | ||
59 | textvar1 = self.bitList[0] # Sets the text of text variable to the 1 or 0 generated in randomByte | |
60 | self.binary_text_var1.set(textvar1) # Sets the text to the text variable | |
61 | print(str(self.bitList[0])) | |
62 | ||
63 | textvar2 = self.bitList[1] | |
64 | self.binary_text_var2.set(textvar2) | |
65 | print(str(self.bitList[1])) | |
66 | ||
67 | textvar3 = self.bitList[2] | |
68 | self.binary_text_var3.set(textvar3) | |
69 | print(str(self.bitList[2])) | |
70 | ||
71 | textvar4 = self.bitList[3] | |
72 | self.binary_text_var4.set(textvar4) | |
73 | print(str(self.bitList[3])) | |
74 | ||
75 | textvar5 = self.bitList[4] | |
76 | self.binary_text_var5.set(textvar5) | |
77 | print(str(self.bitList[4])) | |
78 | ||
79 | textvar6 = self.bitList[5] | |
80 | self.binary_text_var6.set(textvar6) | |
81 | print(str(self.bitList[5])) | |
82 | ||
83 | textvar7 = self.bitList[6] | |
84 | self.binary_text_var7.set(textvar7) | |
85 | print(str(self.bitList[6])) | |
86 | ||
87 | textvar8 = self.bitList[7] | |
88 | self.binary_text_var8.set(textvar8) | |
89 | print(str(self.bitList[7])) | |
90 | ||
91 | def callFunctions(self): # Calls on both randomByte and setText functions | |
92 | ||
93 | Application.randomByte(self) | |
94 | Application.setText(self) | |
95 | ||
96 | if Application.callIf[0] == True: # Calls if countdown has already reached 0 | |
97 | Application.countdown(self, 6) | |
98 | ||
99 | def inputFunctions(self, button): | |
100 | ||
101 | if Application.callIf[0] == True: # Only lets input if countdown is equal to 0 | |
102 | ||
103 | Application.inputList.append(button) | |
104 | self.concat = ''.join(Application.inputList) # Concatenates the numbers in the inputList to a single number | |
105 | self.buttonNumber = self.concat # Sets buttonNumber equal to the joined numbers | |
106 | self.answerText.config(text=self.buttonNumber) # Sets the text to the buttonNumber | |
107 | ||
108 | def clearInput(self): # Allows the input to be cleared with the clear button | |
109 | ||
110 | try: | |
111 | Application.inputList.clear() #Clerars the inputList | |
112 | except: | |
113 | self.inputList[:] = [] | |
114 | ||
115 | self.buttonNumber = '0' # Sets buttonNumber to an empty string | |
116 | self.answerText.config(text=self.buttonNumber) # Sets the texty to the empt y string | |
117 | ||
118 | def enterButton(self): | |
119 | ||
120 | fullByte = int(Application.byteCache, 2) # Grabs the fullbyte from the byteCache | |
121 | answerByte = self.concat # Sets answerByte to the concatenated number | |
122 | print(fullByte) | |
123 | print(answerByte ) | |
124 | ||
125 | if fullByte == int(answerByte): # Checks if the answer is the same as the binary number shown on display | |
126 | Application.score += 1 # Adds score plus 1 | |
127 | self.scoreText.config(text='Score:' + str(Application.score)) # Sets text of score | |
128 | ||
129 | Application.callFunctions(self) # Calls on function to allow another byte to be solved by the user | |
130 | ||
131 | ||
132 | '''def toolBar(self, master): # Creates the toolbar as well as the dropdown menu | |
133 | ||
134 | self.selectionMenu = Menu(master) | |
135 | master.config(menu=self.selectionMenu) | |
136 | ||
137 | self.menuParams = Menu(self.selectionMenu) | |
138 | self.selectionMenu.add_cascade(label='Options', menu=self.menuParams) | |
139 | self.menuParams.add_command(label='About')''' | |
140 | ||
141 | def restartGame(self): # Allows user to restart game if stuck on problem | |
142 | ||
143 | Application.score = 0 # Sets score back to zero | |
144 | self.scoreText.config(text='Score:' + str(Application.score)) # Sets text to zero | |
145 | ||
146 | Application.clearInput(self) # Clears the input | |
147 | ||
148 | for i in range(0,8): # Clears the list | |
149 | self.bitList[i] = 0 | |
150 | ||
151 | Application.setText(self) # sets text for boxes back to zero | |
152 | ||
153 | ||
154 | def topElements(self, master): # Creates the top elements such as the top frame and the time text for when the timer counts down for a visual | |
155 | ||
156 | self.topFrame = Frame(master) # Frame that houses the top elements | |
157 | self.topFrame.grid(row=0, column = 0, sticky='w') | |
158 | ||
159 | self.timeText = Label(self.topFrame, text='Time:') | |
160 | self.timeText.grid() | |
161 | ||
162 | self.timeText_n = Label(self.topFrame, text=0) | |
163 | self.timeText_n.grid(row=0, column=1) | |
164 | ||
165 | self.topFrame_Clear = Frame(master) | |
166 | self.topFrame_Clear.grid(row=0, sticky='e') | |
167 | ||
168 | self.clearButton = Button(self.topFrame_Clear, text='Clear', command=lambda : Application.clearInput(self)) | |
169 | self.clearButton.grid() | |
170 | ||
171 | def binaryBox_frame(self, master): # Creates the textboxes that show the binary numbers | |
172 | ||
173 | self.binaryFrame = Frame(master) # Frame that houses the binary Elements | |
174 | self.binaryFrame.grid(row=1) | |
175 | ||
176 | self.binary_text_var1 = StringVar() # binary text variable set as object from tkinter | |
177 | self.binary_text_var1.set('0') # Sets default of 0 | |
178 | self.binaryLabel1 = Label(self.binaryFrame, textvariable=self.binary_text_var1, borderwidth=5, relief='ridge',) | |
179 | self.binaryLabel1.grid(row=0, column=0) | |
180 | self.binaryLabel1.config(font=('Courier', 25)) # Size and font type of the text | |
181 | ||
182 | self.binary_text_var2 = StringVar() | |
183 | self.binary_text_var2.set('0') | |
184 | self.binaryLabel2 = Label(self.binaryFrame, textvariable=self.binary_text_var2, borderwidth=5, relief='ridge') | |
185 | self.binaryLabel2.grid(row=0, column=1) | |
186 | self.binaryLabel2.config(font=('Courier', 25)) | |
187 | ||
188 | self.binary_text_var3 = StringVar() | |
189 | self.binary_text_var3.set('0') | |
190 | self.binaryLabel3 = Label(self.binaryFrame, textvariable=self.binary_text_var3, borderwidth=5, relief='ridge') | |
191 | self.binaryLabel3.grid(row=0, column=2) | |
192 | self.binaryLabel3.config(font=('Courier', 25)) | |
193 | ||
194 | self.binary_text_var4 = StringVar() | |
195 | self.binary_text_var4.set('0') | |
196 | self.binaryLabel4 = Label(self.binaryFrame, textvariable=self.binary_text_var4, borderwidth=5, relief='ridge') | |
197 | self.binaryLabel4.grid(row=0, column=3) | |
198 | self.binaryLabel4.config(font=('Courier', 25)) | |
199 | ||
200 | self.binary_text_var5 = StringVar() | |
201 | self.binary_text_var5.set('0') | |
202 | self.binaryLabel5 = Label(self.binaryFrame, textvariable=self.binary_text_var5, borderwidth=5, relief='ridge') | |
203 | self.binaryLabel5.grid(row=0, column=4) | |
204 | self.binaryLabel5.config(font=('Courier', 25)) | |
205 | ||
206 | self.binary_text_var6 = StringVar() | |
207 | self.binary_text_var6.set('0') | |
208 | self.binaryLabel6 = Label(self.binaryFrame, textvariable=self.binary_text_var6, borderwidth=5, relief='ridge') | |
209 | self.binaryLabel6.grid(row=0, column=5) | |
210 | self.binaryLabel6.config(font=('Courier', 25)) | |
211 | ||
212 | self.binary_text_var7 = StringVar() | |
213 | self.binary_text_var7.set('0') | |
214 | self.binaryLabel7 = Label(self.binaryFrame, textvariable=self.binary_text_var7, borderwidth=5, relief='ridge') | |
215 | self.binaryLabel7.grid(row=0, column=6) | |
216 | self.binaryLabel7.config(font=('Courier', 25)) | |
217 | ||
218 | self.binary_text_var8 = StringVar() | |
219 | self.binary_text_var8.set('0') | |
220 | self.binaryLabel8 = Label(self.binaryFrame, textvariable=self.binary_text_var8, borderwidth=5, relief='ridge') | |
221 | self.binaryLabel8.grid(row=0, column=7) | |
222 | self.binaryLabel8.config(font=('Courier', 25)) | |
223 | ||
224 | def bottomElements(self, master): # Creates the bottom elements such as the keypad and start and enter buttons | |
225 | ||
226 | self.bottomFrame = Frame(master) | |
227 | self.bottomFrame.grid(row=2) | |
228 | ||
229 | self.answerText = Label(self.bottomFrame, text='', height=1, width=20, bg='white', relief='ridge') | |
230 | self.answerText.grid(row=0, column=4, columnspan=5, pady=5) | |
231 | ||
232 | self.button1= Button(self.bottomFrame, text = 1, height=2, width=5, command= lambda : Application.inputFunctions(self, '1')) | |
233 | self.button1.grid(row=1, column=5, sticky='e') | |
234 | ||
235 | self.button2 = Button(self.bottomFrame, text = 2, height=2, width=5, command= lambda : Application.inputFunctions(self, '2')) | |
236 | self.button2.grid(row=1, column=6) | |
237 | ||
238 | self.button3 = Button(self.bottomFrame, text = 3, height=2, width=5, command= lambda : Application.inputFunctions(self, '3')) | |
239 | self.button3.grid(row=1, column=7, sticky='w') | |
240 | ||
241 | self.button4 = Button(self.bottomFrame, text = 4, height=2, width=5, command= lambda : Application.inputFunctions(self, '4')) | |
242 | self.button4.grid(row=2, column=5, sticky='e') | |
243 | ||
244 | self.button5 = Button(self.bottomFrame, text = 5, height=2, width=5, command= lambda : Application.inputFunctions(self, '5')) | |
245 | self.button5.grid(row=2, column=6) | |
246 | ||
247 | self.button6 = Button(self.bottomFrame, text = 6, height=2, width=5, command= lambda : Application.inputFunctions(self, '6')) | |
248 | self.button6.grid(row=2, column=7, sticky='w') | |
249 | ||
250 | self.button7 = Button(self.bottomFrame, text = 7, height=2, width=5, command= lambda : Application.inputFunctions(self, '7')) | |
251 | self.button7.grid(row=3, column=5, sticky='e') | |
252 | ||
253 | self.button8 = Button(self.bottomFrame, text = 8, height=2, width=5, command= lambda : Application.inputFunctions(self, '8')) | |
254 | self.button8.grid(row=3, column=6) | |
255 | ||
256 | self.button9 = Button(self.bottomFrame, text = 9, height=2, width=5, command= lambda : Application.inputFunctions(self, '9')) | |
257 | self.button9.grid(row=3, column=7, sticky='w') | |
258 | ||
259 | self.buttonStart = Button(self.bottomFrame, text = 'Start', height=2, width=5, command= lambda : Application.callFunctions(self)) | |
260 | self.buttonStart.grid(row=4, column=5, sticky='e') | |
261 | ||
262 | self.button0 = Button(self.bottomFrame, text = 0, height=2, width=5, command= lambda : Application.inputFunctions(self, '0')) | |
263 | self.button0.grid(row=4, column=6) | |
264 | ||
265 | self.buttonEnter = Button(self.bottomFrame, text = 'Enter', height=2, width=5, command= lambda : Application.enterButton(self)) | |
266 | self.buttonEnter.grid(row=4, column=7, sticky='w') | |
267 | ||
268 | self.scoreText = Label(master, text='Score:') | |
269 | self.scoreText.grid(row=2, column=0, stick=W+S) | |
270 | ||
271 | restartButton = Button(master, text = 'Restart', height=1, width=4, command= lambda : Application.restartGame(self)) | |
272 | restartButton.grid(row=2, column=0, sticky=E+S) | |
273 | ||
274 | # Code below are objects that call on the functions in the class to use in tkinter for the GUI | |
275 | ||
276 | master = Tk() | |
277 | ||
278 | binaryGame = Application() | |
279 | ||
280 | '''binaryGame.toolBar(master)''' | |
281 | ||
282 | binaryGame.topElements(master) | |
283 | ||
284 | binaryGame.binaryBox_frame(master) | |
285 | ||
286 | binaryGame.bottomElements(master) | |
287 | ||
288 | master.mainloop() |