from tkinter import *
import random
import os
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
self.variables()
self.letterListGrab()
root.bind('<Return>', self.rounds)
self.countdown()
def countdown(self):
if (self.timerStart == True):
self.label.configure(text= round(self.remaining, 2))
self.remaining = self.remaining + .01
self.after(10, self.countdown)
def create_widgets(self):
self.pair = Label(self, text="Helvetica", font=("Helvetica", 30))
self.pair["text"] = " "
self.pair.grid(row=0, column=0, columnspan=3, pady=30, padx=30)
self.hintLab = Label(self)
self.hintLab["text"] = ""
self.hintLab.grid(row=1, columnspan=3)
self.instr0 = Label(self)
self.instr0["text"] = "How many pairs would you like to memorize:"
self.instr0.grid(row=2, column=0, columnspan=3)
self.countEntry = Entry(self)
self.countEntry["text"] = "5"
self.countEntry.grid(row=3, column=0, columnspan=2)
self.start = Button(self)
self.start["text"] = "Start"
self.start["command"] = self.runGame
self.start.grid(row=3, column=2)
self.result = Label(self, wraplength=280)
self.result["text"] = ""
self.result.grid(row=4, column=0, columnspan=3)
self.label = Label(self, text="Helvetica", font=("Helvetica", 20))
self.label["text"] = "0.00"
self.label.grid(row=5, columnspan=3, pady=30, padx=30)
def variables(self):
self.pairCount = 5
self.tally = 0
self.answer = ""
self.remaining = 0.0
self.timerStart = False
def runGame(self):
self.timerStart = True
self.pairCount = int(self.countEntry.get())
self.instr0.destroy()
self.start.destroy()
self.countEntry.destroy()
self.instr1 = Label(self)
self.instr1["text"] = "Press Enter to Continue"
self.instr1.grid(row=2, column=0, columnspan=3)
self.hintry = Entry(self)
self.hintry["text"] = ""
self.hintry.grid(row=3, column=0, columnspan=2)
self.hintSub = Button(self)
self.hintSub["text"] = "Add Hint"
self.hintSub["command"] = self.addHint
self.hintSub.grid(row=3, column=2)
self.hintLab["text"] = ""
self.pairNum = randPair(self.letterList)
self.curPair = str(self.letterList[self.pairNum][0])
self.answer += self.curPair
self.pair["text"] = self.curPair
for x in range (1, len(self.letterList[self.pairNum])):
self.hintLab["text"] = "Hint " + str(x) + ": " + self.letterList[self.pairNum][x]
self.tally += 1
def rounds(self, event):
self.hintLab["text"] = ""
if (self.tally == 0):
return
elif (self.tally < self.pairCount):
self.pairNum = randPair(self.letterList)
self.curPair = str(self.letterList[self.pairNum][0])
self.answer += self.curPair
self.pair["text"] = self.curPair
for x in range (1, len(self.letterList[self.pairNum])):
self.hintLab["text"] += "Hint " + str(x) + ": " + self.letterList[self.pairNum][x] + "\n"
self.tally += 1
elif (self.tally == self.pairCount):
self.checkAnswer()
self.tally += 1
else:
return
def checkAnswer(self):
self.start.destroy()
self.pair["text"] = " "
self.instr1.destroy()
self.hintry.destroy()
self.hintSub.destroy()
self.ansPrompt = Label(self)
self.ansPrompt["text"] = "Type in the answer:"
self.ansPrompt.grid(row=2, column=0, columnspan=3)
self.ansEntry = Entry(self)
self.ansEntry["text"] = ""
self.ansEntry.grid(row=3, column=0, columnspan=2)
self.submit = Button(self)
self.submit["text"] = "Submit"
self.submit["command"] = self.compare
self.submit.grid(row=3, column=2)
def compare(self):
self.totPairs = int(len(self.answer)/2)
self.timerStart = False
self.userAnswer = self.ansEntry.get()
self.userAnswer = self.userAnswer.upper()
self.userAnswer = self.userAnswer.replace(" ", "")
self.ansPrompt.destroy()
self.ansEntry.destroy()
self.submit.destroy()
self.resetBut = Button(self)
self.resetBut["text"] = "Reset"
self.resetBut["command"] = self.reset
self.resetBut.grid(row=3, column=2)
if (self.userAnswer == self.answer):
self.result["text"] = "Congratulations! You memorized " + str(self.totPairs) + " pairs."
else:
self.result["text"] = "Sorry, the answer was " + self.answer + ", but you typed " + self.userAnswer + "."
self.submit.destroy()
def reset(self):
self.destroy()
Application(root)
def letterListGrab(self):
#checks to see if the letterList text file already exists
fileCheck = os.path.isfile("letterList.txt")
#if the file exists, process the file, if not create a new file
if (fileCheck == True):
self.letterList = processFile()
else:
self.letterList = createTable()
def addHint(self):
self.addHint = str(self.hintry.get())
if (self.addHint != ""):
self.letterList[self.pairNum].append(self.addHint)
writeFile(self.letterList)
def randPair(letterList):
roundRand = random.randint(0,len(letterList))
return roundRand
def createTable():
letterList = []
#shift to the upper case letter index
shift = 65
#runs through the first letter
for firstLet in range(0, 24):
#nexted for loop to pair the first letter with a second letter
for secondLet in range(0,24):
#if statement ensure no double same letter pairs (i.e. "AA")
if (firstLet != secondLet):
letPair = (chr(firstLet+shift)+chr(secondLet+shift))
#appends a list so more information can be added to a letter pair
letterList.append([letPair])
writeFile(letterList)
return letterList
def writeFile(letterList):
file = open("letterList.txt", "w")
for line in range(0, len(letterList)):
for element in range(0, len(letterList[line])):
file.write(letterList[line][element])
#adds a ", " if it's not the final element of the list
if (element+1 < len(letterList[line])):
file.write(", ")
file.write("\n")
file.close()
def processFile():
letterList = []
#loads the file into a list of the lines
with open("letterList.txt") as f:
lines = f.readlines()
#runs each line, cutting off the line break, and splitting by the comma space.
for line in range(0, len(lines)):
raw = lines[line].rstrip()
rawList = raw.split(", ")
letterList.append(rawList)
return letterList
root = Tk()
root.title("Letter Pair Game")
root.geometry("300x300")
app = Application(root)
root.mainloop()