spacepaste

  1.  
  2. import tkinter as tk
  3. import time, random, math, winsound,threading
  4. class Question():
  5. labela = None
  6. labelb = None
  7. labelmath = None
  8. answer = None
  9. a = 0
  10. b = 0
  11. math = " "
  12. numAnswer = 0
  13. elements = None
  14. def isPrime(self, num):
  15. if num <= 2:
  16. return False
  17. for i in range(2, int(math.sqrt(num)) + 1):
  18. if num % i == 0:
  19. return False
  20. return True
  21. def findAnswer(self):
  22. if self.type == 1:
  23. self.numAnswer = self.a + self.b
  24. elif self.type == 2:
  25. self.numAnswer = self.a - self.b
  26. elif self.type == 3:
  27. self.numAnswer = self.a * self.b
  28. elif self.type == 4:
  29. self.numAnswer = int(self.a / self.b)
  30. def __init__(self, window):
  31. self.labela = tk.Label(window, font=("Courier", 12))
  32. self.labelb = tk.Label(window, font=("Courier", 12))
  33. self.labelmath = tk.Label(window, font=("Courier", 12))
  34. self.answer = tk.Entry(window, width=3)
  35. self.type = random.randint(1, 4)
  36. if self.type == 1:
  37. self.a = random.randint(1, 50)
  38. self.b = random.randint(1, 50)
  39. self.math = "+"
  40. elif self.type == 2:
  41. self.a = random.randint(1, 50)
  42. self.b = random.randint(1, 50)
  43. if self.a < self.b:
  44. self.a, self.b = self.b, self.a
  45. self.math = "-"
  46. elif self.type == 3:
  47. self.a = random.randint(1, 13)
  48. self.b = random.randint(1, 13)
  49. self.math = "*"
  50. elif self.type == 4:
  51. prime = True
  52. self.a = 0
  53. while prime:
  54. self.a = random.randint(20, 144)
  55. prime = self.isPrime(self.a)
  56. divisors = []
  57. for number in range(2, self.a + 1):
  58. if self.a % number == 0:
  59. divisors.append(number)
  60. random.shuffle(divisors)
  61. self.b = divisors.pop()
  62. self.math = "/"
  63. self.labela["text"] = str(self.a)
  64. self.labelmath["text"] = self.math
  65. self.labelb["text"] = str(self.b)
  66. self.elements = [self.labela, self.labelmath, self.labelb, self.answer]
  67. self.findAnswer()
  68. class App():
  69. currentrow = 0
  70. correct = 0
  71. numquestions = 80
  72. maxcol = 0
  73. focus = False
  74. resetrow = 0
  75. currentsecond = 0
  76. def __init__(self):
  77. self.root = tk.Tk()
  78. self.root.winfo_toplevel().title("Math Game")
  79. #labeltitle = tk.Label(self.root, text="Quickly now", font=("Courier", 44))
  80. #self.currentrow += 1
  81. self.timerlabel = tk.Label(self.root, text="", font=("Courier", 32))
  82. self.currentrow += 1
  83. self.then = 60+time.time()
  84. self.resetrow = self.currentrow
  85. self.setupquestions()
  86. self.timerlabel.grid(row=0, columnspan=self.maxcol)
  87. # ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
  88. self.update_clock()
  89. self.root.mainloop()
  90. def beep(self):
  91. winsound.Beep(7000,100)
  92. def update_clock(self):
  93. now = time.time()
  94. currenttime = self.then - now
  95. if self.currentsecond == 0:
  96. self.currentsecond = int(round(self.then - now,0))
  97. if currenttime < 0:
  98. self.timerlabel.configure(fg="black")
  99. currenttime = self.correct
  100. elif int((currenttime*100)) <= 5:
  101. self.check()
  102. elif currenttime <= 10:
  103. self.timerlabel.configure(fg="red")
  104. if self.currentsecond - int(currenttime) > 0:
  105. self.currentsecond -=1
  106. t = threading.Thread(target=self.beep)
  107. t.start()
  108. self.timerlabel.configure(text="{:04.2f}".format(currenttime))
  109. self.root.after(1, self.update_clock)
  110. def check(self):
  111. self.timerlabel.configure(text=0)
  112. entries = 0
  113. for c in self.root.winfo_children():
  114. if isinstance(c, tk.Entry):
  115. if len(c.get()) > 0:
  116. if int(c.get()) == self.questions[entries].numAnswer:
  117. self.correct += 1
  118. c.config(disabledbackground="green")
  119. else:
  120. c.config(disabledbackground="red")
  121. #print("got {} should be {}".format(c.get(), self.questions[entries].numAnswer))
  122. else:
  123. c.config(disabledbackground="red")
  124. entries += 1
  125. self.timerlabel.configure(fg="black")
  126. self.timerlabel.configure(text=self.correct)
  127. self.timeout()
  128. #print("{} {}/{}".format(int((self.correct / self.numquestions) * 100.0), self.correct, self.numquestions))
  129. def setupquestions(self):
  130. self.questions = []
  131. rowsofquestions = 10
  132. for i in range(self.numquestions):
  133. self.questions.append(Question(self.root))
  134. for i in range(0, len(self.questions), int(self.numquestions / rowsofquestions)):
  135. col = 0
  136. for k in range(0, int(self.numquestions / rowsofquestions)):
  137. size = len(self.questions[i + k].elements)
  138. for element in self.questions[i + k].elements:
  139. if isinstance(element, tk.Entry) and not self.focus:
  140. element.focus_set()
  141. self.focus = True
  142. if not isinstance(element,tk.Entry):
  143. element.grid(row=self.currentrow, column=col, pady=(0, 20))
  144. elif k != int(self.numquestions / rowsofquestions ) -1:
  145. element.grid(row=self.currentrow, column=col, pady=(0, 20),padx=(10,30))
  146. elif k == 0:
  147. element.grid(row=self.currentrow, column=col, pady=(0, 20), padx=(30,10))
  148. else:
  149. element.grid(row=self.currentrow, column=col, pady=(0, 20))
  150. col += 1
  151. if col > self.maxcol:
  152. self.maxcol = col
  153. self.currentrow += 1
  154. # print("[{}] {} {} {}".format(question.type,question.a,question.math,question.b))
  155. def timeout(self):
  156. for q in self.questions:
  157. q.answer.config(state="disabled")
  158. app = App()
  159.