Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- from tkinter import *
- except:
- from Tkinter import *
- import random
- class Application:
- concat = []
- inputList = [] # Gets the input from the keypad and the answer text
- callIf = [True] # Checks if the countdown is at zero and allows other functions to be called when condition is met
- bit = {} # Dictonary that houses the bits
- bitList = [] # List that takes data from the bit dictionary and passes it to the tkinter Labels
- byteCache = '' # Stores the byte so it can be compared to the answerbyte and no errors are made in the comparison
- score = 0 # Keeps the score of when the user gets a byte right
- def __init__(self):
- master.wm_title("Binary Math") # Sets name of the Window
- def countdown(self, number):
- number -= 1 # subtracts 1 from number for countdown
- self.timeText_n.config(text=number) # Sets text to variable number
- if number <=0: # Checks If timer is less than or equal to zero
- self.timeText_n.config(text='Times Up!') # Changes text
- 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
- for i in range(0,8): # Sets everything in bitList to 0
- self.bitList[i] = 0
- Application.setText(self) # Sets all the text to 0 of the binaryBoxes
- else:
- master.after(1000, self.countdown, number) # If timer is not less then zero it counts until it is zero
- Application.callIf[0] = False
- 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'
- if Application.callIf[0] == True:
- try:
- self.bitList.clear() # Clears anything that may be in the bitList
- except:
- self.bitList[:]=[]
- 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
- self.bit['bit' + str(i)] = random.randint(0,1)
- self.bitList.append(str(self.bit['bit' + str(i)]))
- Application.byteCache = ''.join(self.bitList) # Saves the byte to byteCache to be used later and not cause any unintended errors
- print(self.bit)
- print(self.bitList)
- print(Application.byteCache)
- def setText(self): # Function sets the text when the start button is clicked
- textvar1 = self.bitList[0] # Sets the text of text variable to the 1 or 0 generated in randomByte
- self.binary_text_var1.set(textvar1) # Sets the text to the text variable
- print(str(self.bitList[0]))
- textvar2 = self.bitList[1]
- self.binary_text_var2.set(textvar2)
- print(str(self.bitList[1]))
- textvar3 = self.bitList[2]
- self.binary_text_var3.set(textvar3)
- print(str(self.bitList[2]))
- textvar4 = self.bitList[3]
- self.binary_text_var4.set(textvar4)
- print(str(self.bitList[3]))
- textvar5 = self.bitList[4]
- self.binary_text_var5.set(textvar5)
- print(str(self.bitList[4]))
- textvar6 = self.bitList[5]
- self.binary_text_var6.set(textvar6)
- print(str(self.bitList[5]))
- textvar7 = self.bitList[6]
- self.binary_text_var7.set(textvar7)
- print(str(self.bitList[6]))
- textvar8 = self.bitList[7]
- self.binary_text_var8.set(textvar8)
- print(str(self.bitList[7]))
- def callFunctions(self): # Calls on both randomByte and setText functions
- Application.randomByte(self)
- Application.setText(self)
- if Application.callIf[0] == True: # Calls if countdown has already reached 0
- Application.countdown(self, 6)
- def inputFunctions(self, button):
- if Application.callIf[0] == True: # Only lets input if countdown is equal to 0
- Application.inputList.append(button)
- self.concat = ''.join(Application.inputList) # Concatenates the numbers in the inputList to a single number
- self.buttonNumber = self.concat # Sets buttonNumber equal to the joined numbers
- self.answerText.config(text=self.buttonNumber) # Sets the text to the buttonNumber
- def clearInput(self): # Allows the input to be cleared with the clear button
- try:
- Application.inputList.clear() #Clerars the inputList
- except:
- self.inputList[:] = []
- self.buttonNumber = '0' # Sets buttonNumber to an empty string
- self.answerText.config(text=self.buttonNumber) # Sets the texty to the empt y string
- def enterButton(self):
- fullByte = int(Application.byteCache, 2) # Grabs the fullbyte from the byteCache
- answerByte = self.concat # Sets answerByte to the concatenated number
- print(fullByte)
- print(answerByte )
- if fullByte == int(answerByte): # Checks if the answer is the same as the binary number shown on display
- Application.score += 1 # Adds score plus 1
- self.scoreText.config(text='Score:' + str(Application.score)) # Sets text of score
- Application.callFunctions(self) # Calls on function to allow another byte to be solved by the user
- '''def toolBar(self, master): # Creates the toolbar as well as the dropdown menu
- self.selectionMenu = Menu(master)
- master.config(menu=self.selectionMenu)
- self.menuParams = Menu(self.selectionMenu)
- self.selectionMenu.add_cascade(label='Options', menu=self.menuParams)
- self.menuParams.add_command(label='About')'''
- def restartGame(self): # Allows user to restart game if stuck on problem
- Application.score = 0 # Sets score back to zero
- self.scoreText.config(text='Score:' + str(Application.score)) # Sets text to zero
- Application.clearInput(self) # Clears the input
- for i in range(0,8): # Clears the list
- self.bitList[i] = 0
- Application.setText(self) # sets text for boxes back to zero
- 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
- self.topFrame = Frame(master) # Frame that houses the top elements
- self.topFrame.grid(row=0, column = 0, sticky='w')
- self.timeText = Label(self.topFrame, text='Time:')
- self.timeText.grid()
- self.timeText_n = Label(self.topFrame, text=0)
- self.timeText_n.grid(row=0, column=1)
- self.topFrame_Clear = Frame(master)
- self.topFrame_Clear.grid(row=0, sticky='e')
- self.clearButton = Button(self.topFrame_Clear, text='Clear', command=lambda : Application.clearInput(self))
- self.clearButton.grid()
- def binaryBox_frame(self, master): # Creates the textboxes that show the binary numbers
- self.binaryFrame = Frame(master) # Frame that houses the binary Elements
- self.binaryFrame.grid(row=1)
- self.binary_text_var1 = StringVar() # binary text variable set as object from tkinter
- self.binary_text_var1.set('0') # Sets default of 0
- self.binaryLabel1 = Label(self.binaryFrame, textvariable=self.binary_text_var1, borderwidth=5, relief='ridge',)
- self.binaryLabel1.grid(row=0, column=0)
- self.binaryLabel1.config(font=('Courier', 25)) # Size and font type of the text
- self.binary_text_var2 = StringVar()
- self.binary_text_var2.set('0')
- self.binaryLabel2 = Label(self.binaryFrame, textvariable=self.binary_text_var2, borderwidth=5, relief='ridge')
- self.binaryLabel2.grid(row=0, column=1)
- self.binaryLabel2.config(font=('Courier', 25))
- self.binary_text_var3 = StringVar()
- self.binary_text_var3.set('0')
- self.binaryLabel3 = Label(self.binaryFrame, textvariable=self.binary_text_var3, borderwidth=5, relief='ridge')
- self.binaryLabel3.grid(row=0, column=2)
- self.binaryLabel3.config(font=('Courier', 25))
- self.binary_text_var4 = StringVar()
- self.binary_text_var4.set('0')
- self.binaryLabel4 = Label(self.binaryFrame, textvariable=self.binary_text_var4, borderwidth=5, relief='ridge')
- self.binaryLabel4.grid(row=0, column=3)
- self.binaryLabel4.config(font=('Courier', 25))
- self.binary_text_var5 = StringVar()
- self.binary_text_var5.set('0')
- self.binaryLabel5 = Label(self.binaryFrame, textvariable=self.binary_text_var5, borderwidth=5, relief='ridge')
- self.binaryLabel5.grid(row=0, column=4)
- self.binaryLabel5.config(font=('Courier', 25))
- self.binary_text_var6 = StringVar()
- self.binary_text_var6.set('0')
- self.binaryLabel6 = Label(self.binaryFrame, textvariable=self.binary_text_var6, borderwidth=5, relief='ridge')
- self.binaryLabel6.grid(row=0, column=5)
- self.binaryLabel6.config(font=('Courier', 25))
- self.binary_text_var7 = StringVar()
- self.binary_text_var7.set('0')
- self.binaryLabel7 = Label(self.binaryFrame, textvariable=self.binary_text_var7, borderwidth=5, relief='ridge')
- self.binaryLabel7.grid(row=0, column=6)
- self.binaryLabel7.config(font=('Courier', 25))
- self.binary_text_var8 = StringVar()
- self.binary_text_var8.set('0')
- self.binaryLabel8 = Label(self.binaryFrame, textvariable=self.binary_text_var8, borderwidth=5, relief='ridge')
- self.binaryLabel8.grid(row=0, column=7)
- self.binaryLabel8.config(font=('Courier', 25))
- def bottomElements(self, master): # Creates the bottom elements such as the keypad and start and enter buttons
- self.bottomFrame = Frame(master)
- self.bottomFrame.grid(row=2)
- self.answerText = Label(self.bottomFrame, text='', height=1, width=20, bg='white', relief='ridge')
- self.answerText.grid(row=0, column=4, columnspan=5, pady=5)
- self.button1= Button(self.bottomFrame, text = 1, height=2, width=5, command= lambda : Application.inputFunctions(self, '1'))
- self.button1.grid(row=1, column=5, sticky='e')
- self.button2 = Button(self.bottomFrame, text = 2, height=2, width=5, command= lambda : Application.inputFunctions(self, '2'))
- self.button2.grid(row=1, column=6)
- self.button3 = Button(self.bottomFrame, text = 3, height=2, width=5, command= lambda : Application.inputFunctions(self, '3'))
- self.button3.grid(row=1, column=7, sticky='w')
- self.button4 = Button(self.bottomFrame, text = 4, height=2, width=5, command= lambda : Application.inputFunctions(self, '4'))
- self.button4.grid(row=2, column=5, sticky='e')
- self.button5 = Button(self.bottomFrame, text = 5, height=2, width=5, command= lambda : Application.inputFunctions(self, '5'))
- self.button5.grid(row=2, column=6)
- self.button6 = Button(self.bottomFrame, text = 6, height=2, width=5, command= lambda : Application.inputFunctions(self, '6'))
- self.button6.grid(row=2, column=7, sticky='w')
- self.button7 = Button(self.bottomFrame, text = 7, height=2, width=5, command= lambda : Application.inputFunctions(self, '7'))
- self.button7.grid(row=3, column=5, sticky='e')
- self.button8 = Button(self.bottomFrame, text = 8, height=2, width=5, command= lambda : Application.inputFunctions(self, '8'))
- self.button8.grid(row=3, column=6)
- self.button9 = Button(self.bottomFrame, text = 9, height=2, width=5, command= lambda : Application.inputFunctions(self, '9'))
- self.button9.grid(row=3, column=7, sticky='w')
- self.buttonStart = Button(self.bottomFrame, text = 'Start', height=2, width=5, command= lambda : Application.callFunctions(self))
- self.buttonStart.grid(row=4, column=5, sticky='e')
- self.button0 = Button(self.bottomFrame, text = 0, height=2, width=5, command= lambda : Application.inputFunctions(self, '0'))
- self.button0.grid(row=4, column=6)
- self.buttonEnter = Button(self.bottomFrame, text = 'Enter', height=2, width=5, command= lambda : Application.enterButton(self))
- self.buttonEnter.grid(row=4, column=7, sticky='w')
- self.scoreText = Label(master, text='Score:')
- self.scoreText.grid(row=2, column=0, stick=W+S)
- restartButton = Button(master, text = 'Restart', height=1, width=4, command= lambda : Application.restartGame(self))
- restartButton.grid(row=2, column=0, sticky=E+S)
- # Code below are objects that call on the functions in the class to use in tkinter for the GUI
- master = Tk()
- binaryGame = Application()
- '''binaryGame.toolBar(master)'''
- binaryGame.topElements(master)
- binaryGame.binaryBox_frame(master)
- binaryGame.bottomElements(master)
- master.mainloop()
Add Comment
Please, Sign In to add comment