import tkinter as tk import time, random, math, winsound,threading class Question(): labela = None labelb = None labelmath = None answer = None a = 0 b = 0 math = " " numAnswer = 0 elements = None def isPrime(self, num): if num <= 2: return False for i in range(2, int(math.sqrt(num)) + 1): if num % i == 0: return False return True def findAnswer(self): if self.type == 1: self.numAnswer = self.a + self.b elif self.type == 2: self.numAnswer = self.a - self.b elif self.type == 3: self.numAnswer = self.a * self.b elif self.type == 4: self.numAnswer = int(self.a / self.b) def __init__(self, window): self.labela = tk.Label(window, font=("Courier", 12)) self.labelb = tk.Label(window, font=("Courier", 12)) self.labelmath = tk.Label(window, font=("Courier", 12)) self.answer = tk.Entry(window, width=3) self.type = random.randint(1, 4) if self.type == 1: self.a = random.randint(1, 50) self.b = random.randint(1, 50) self.math = "+" elif self.type == 2: self.a = random.randint(1, 50) self.b = random.randint(1, 50) if self.a < self.b: self.a, self.b = self.b, self.a self.math = "-" elif self.type == 3: self.a = random.randint(1, 13) self.b = random.randint(1, 13) self.math = "*" elif self.type == 4: prime = True self.a = 0 while prime: self.a = random.randint(20, 144) prime = self.isPrime(self.a) divisors = [] for number in range(2, self.a + 1): if self.a % number == 0: divisors.append(number) random.shuffle(divisors) self.b = divisors.pop() self.math = "/" self.labela["text"] = str(self.a) self.labelmath["text"] = self.math self.labelb["text"] = str(self.b) self.elements = [self.labela, self.labelmath, self.labelb, self.answer] self.findAnswer() class App(): currentrow = 0 correct = 0 numquestions = 80 maxcol = 0 focus = False resetrow = 0 def __init__(self): self.root = tk.Tk() self.root.winfo_toplevel().title("Math Game") #labeltitle = tk.Label(self.root, text="Quickly now", font=("Courier", 44)) #self.currentrow += 1 self.timerlabel = tk.Label(self.root, text="", font=("Courier", 32)) self.currentrow += 1 self.then = 60+(int(time.time()*100)/100.0) self.resetrow = self.currentrow self.setupquestions() self.timerlabel.grid(row=0, columnspan=self.maxcol) # ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative') self.update_clock() self.root.mainloop() def beep(): winsound.Beep(7000,100) return def update_clock(self): now = int(time.time()*100)/100.0 currenttime = round(self.then - now,2) if currenttime < 0: self.timerlabel.configure(fg="black") currenttime = self.correct elif currenttime == 0: self.check() elif currenttime <= 10: self.timerlabel.configure(fg="red") if (currenttime *100) %100 == 0: t = threading.Thread(target=App.beep) t.start() self.timerlabel.configure(text="{:04.2f}".format(currenttime)) self.root.after(10, self.update_clock) def check(self): self.timerlabel.configure(text=0) entries = 0 for c in self.root.winfo_children(): if isinstance(c, tk.Entry): if len(c.get()) > 0: if int(c.get()) == self.questions[entries].numAnswer: self.correct += 1 c.config(disabledbackground="green") else: c.config(disabledbackground="red") #print("got {} should be {}".format(c.get(), self.questions[entries].numAnswer)) else: c.config(disabledbackground="red") entries += 1 self.timerlabel.configure(fg="black") self.timerlabel.configure(text=self.correct) self.timeout() #print("{} {}/{}".format(int((self.correct / self.numquestions) * 100.0), self.correct, self.numquestions)) def setupquestions(self): self.questions = [] rowsofquestions = 10 for i in range(self.numquestions): self.questions.append(Question(self.root)) for i in range(0, len(self.questions), int(self.numquestions / rowsofquestions)): col = 0 for k in range(0, int(self.numquestions / rowsofquestions)): size = len(self.questions[i + k].elements) for element in self.questions[i + k].elements: if isinstance(element, tk.Entry) and not self.focus: element.focus_set() self.focus = True if not isinstance(element,tk.Entry): element.grid(row=self.currentrow, column=col, pady=(0, 20)) elif k != int(self.numquestions / rowsofquestions ) -1: element.grid(row=self.currentrow, column=col, pady=(0, 20),padx=(10,30)) elif k == 0: element.grid(row=self.currentrow, column=col, pady=(0, 20), padx=(30,10)) else: element.grid(row=self.currentrow, column=col, pady=(0, 20)) col += 1 if col > self.maxcol: self.maxcol = col self.currentrow += 1 # print("[{}] {} {} {}".format(question.type,question.a,question.math,question.b)) def timeout(self): for q in self.questions: q.answer.config(state="disabled") app = App()