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. def __init__(self):
  76. self.root = tk.Tk()
  77. self.root.winfo_toplevel().title("Math Game")
  78. #labeltitle = tk.Label(self.root, text="Quickly now", font=("Courier", 44))
  79. #self.currentrow += 1
  80. self.timerlabel = tk.Label(self.root, text="", font=("Courier", 32))
  81. self.currentrow += 1
  82. self.then = 60+(int(time.time()*100)/100.0)
  83. self.resetrow = self.currentrow
  84. self.setupquestions()
  85. self.timerlabel.grid(row=0, columnspan=self.maxcol)
  86. # ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
  87. self.update_clock()
  88. self.root.mainloop()
  89. def beep():
  90. winsound.Beep(7000,100)
  91. return
  92. def update_clock(self):
  93. now = int(time.time()*100)/100.0
  94. currenttime = round(self.then - now,2)
  95. if currenttime < 0:
  96. self.timerlabel.configure(fg="black")
  97. currenttime = self.correct
  98. elif currenttime == 0:
  99. self.check()
  100. elif currenttime <= 10:
  101. self.timerlabel.configure(fg="red")
  102. if (currenttime *100) %100 == 0:
  103. t = threading.Thread(target=App.beep)
  104. t.start()
  105. self.timerlabel.configure(text="{:04.2f}".format(currenttime))
  106. self.root.after(10, self.update_clock)
  107. def check(self):
  108. self.timerlabel.configure(text=0)
  109. entries = 0
  110. for c in self.root.winfo_children():
  111. if isinstance(c, tk.Entry):
  112. if len(c.get()) > 0:
  113. if int(c.get()) == self.questions[entries].numAnswer:
  114. self.correct += 1
  115. c.config(disabledbackground="green")
  116. else:
  117. c.config(disabledbackground="red")
  118. #print("got {} should be {}".format(c.get(), self.questions[entries].numAnswer))
  119. else:
  120. c.config(disabledbackground="red")
  121. entries += 1
  122. self.timerlabel.configure(fg="black")
  123. self.timerlabel.configure(text=self.correct)
  124. self.timeout()
  125. #print("{} {}/{}".format(int((self.correct / self.numquestions) * 100.0), self.correct, self.numquestions))
  126. def setupquestions(self):
  127. self.questions = []
  128. rowsofquestions = 10
  129. for i in range(self.numquestions):
  130. self.questions.append(Question(self.root))
  131. for i in range(0, len(self.questions), int(self.numquestions / rowsofquestions)):
  132. col = 0
  133. for k in range(0, int(self.numquestions / rowsofquestions)):
  134. size = len(self.questions[i + k].elements)
  135. for element in self.questions[i + k].elements:
  136. if isinstance(element, tk.Entry) and not self.focus:
  137. element.focus_set()
  138. self.focus = True
  139. if not isinstance(element,tk.Entry):
  140. element.grid(row=self.currentrow, column=col, pady=(0, 20))
  141. elif k != int(self.numquestions / rowsofquestions ) -1:
  142. element.grid(row=self.currentrow, column=col, pady=(0, 20),padx=(10,30))
  143. elif k == 0:
  144. element.grid(row=self.currentrow, column=col, pady=(0, 20), padx=(30,10))
  145. else:
  146. element.grid(row=self.currentrow, column=col, pady=(0, 20))
  147. col += 1
  148. if col > self.maxcol:
  149. self.maxcol = col
  150. self.currentrow += 1
  151. # print("[{}] {} {} {}".format(question.type,question.a,question.math,question.b))
  152. def timeout(self):
  153. for q in self.questions:
  154. q.answer.config(state="disabled")
  155. app = App()
  156.