Advertisement
furas

QA Math

Nov 21st, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3.  
  4. class Application(Frame):
  5.    
  6.     def __init__(self, master, *args, **kwargs):
  7.         Frame.__init__(self, master, *args, **kwargs)
  8.        
  9.         self.score = 0
  10.         self.question_count = 0
  11.        
  12.         self.createWidgets()
  13.         self.random_question()
  14.        
  15.     def replaceText(self, text):
  16.         self.display.delete(0, END)
  17.         self.display.insert(0, text)
  18.  
  19.     def calculateExpression(self):
  20.         self.expression = self.display.get()
  21.  
  22.         try:
  23.             self.result = eval(self.expression)
  24.             self.replaceText(self.result)
  25.         except:
  26.             messagebox.showinfo("Error", "Invalid Input")
  27.  
  28.     def appendToDisplay(self, text):
  29.         self.entryText = self.display.get()
  30.         self.textLength = len(self.entryText)
  31.  
  32.         if self.entryText == "0":
  33.             self.replaceText(text)
  34.         else:
  35.             self.display.insert(self.textLength, text)
  36.  
  37.     def clearText(self):
  38.         self.replaceText("0")
  39.  
  40.     def createWidgets(self):
  41.         self.display = Entry(self, font = ("Calibri", 16), borderwidth = 1, relief = RAISED, justify = RIGHT)
  42.         self.display.insert(0, "0")
  43.         self.display.grid(row = 0, column = 0, columnspan = 4)
  44.  
  45.         self.sevenButton = Button(self, font = ("Calibri", 15), text = "7", borderwidth = 1, command = lambda: self.appendToDisplay("7"))
  46.         self.sevenButton.grid(row=1, column=0, sticky="NWNESWSE")
  47.  
  48.         self.eightButton = Button(self, font = ("Calibri", 15), text = "8", borderwidth = 1, command = lambda: self.appendToDisplay("8"))
  49.         self.eightButton.grid(row=1, column=1, sticky="NWNESWSE")
  50.  
  51.         self.nineButton = Button(self, font = ("Calibri", 15), text = "9", borderwidth = 1, command = lambda: self.appendToDisplay("9"))
  52.         self.nineButton.grid(row=1, column=2, sticky="NWNESWSE")
  53.  
  54.         self.clearButton = Button(self, font=("Calibri", 15), text = "C", borderwidth = 1, command = lambda: self.clearText())
  55.         self.clearButton.grid(row = 1, rowspan = 2, column = 3, sticky = "NWNESWSE")
  56.  
  57.  
  58.         self.fourButton = Button(self, font = ("Calibri", 15), text = "4", borderwidth = 1, command = lambda: self.appendToDisplay("4"))
  59.         self.fourButton.grid(row = 2, column = 0, sticky = "NWNESWSE")
  60.  
  61.         self.fiveButton = Button(self, font = ("Calibri", 15), text = "5", borderwidth = 1, command = lambda: self.appendToDisplay("5"))
  62.         self.fiveButton.grid(row = 2, column = 1, sticky = "NWNESWSE")
  63.  
  64.         self.sixButton = Button(self, font = ("Calibri", 15), text = "6", borderwidth = 1, command = lambda: self.appendToDisplay("6"))
  65.         self.sixButton.grid(row = 2, column = 2, sticky = "NWNESWSE")
  66.  
  67.  
  68.         self.oneButton = Button(self, font = ("Calibri", 15), text = "1", borderwidth = 1, command = lambda: self.appendToDisplay("1"))
  69.         self.oneButton.grid(row = 3, column = 0, sticky = "NWNESWSE")
  70.  
  71.         self.twoButton = Button(self, font = ("Calibri", 15), text = "2", borderwidth = 1, command = lambda: self.appendToDisplay("2"))
  72.         self.twoButton.grid(row = 3, column = 1, sticky = "NWNESWSE")
  73.  
  74.         self.threeButton = Button(self, font = ("Calibri", 15), text = "3", borderwidth = 1, command = lambda: self.appendToDisplay("3"))
  75.         self.threeButton.grid(row = 3, column = 2, sticky = "NWNESWSE")
  76.  
  77.         self.OKButton = Button(self, font = ("Calibri", 15), text = "OK", borderwidth = 1, command=self.test_answer)
  78.         self.OKButton.grid(row = 3, rowspan = 2, column = 3, sticky = "NWNESWSE")
  79.  
  80.  
  81.         self.zeroButton = Button(self, font = ("Calibri", 15), text = "0", borderwidth = 1, command = lambda: self.appendToDisplay("0"))
  82.         self.zeroButton.grid(row = 4, column = 0, columnspan = 2, sticky = "NWNESWSE")
  83.  
  84.         self.dotButton = Button(self, font = ("Calibri", 15), text = ".", borderwidth = 1, command = lambda: self.appendToDisplay("."))
  85.         self.dotButton.grid(row = 4, column = 2, sticky = "NWNESWSE")
  86.  
  87.         self.question = Label(self)
  88.         self.question.grid(row=5, column=0, columnspan = 4)
  89.         self.info = Label(self)
  90.         self.info.grid(row=6, column=0, columnspan = 4)
  91.  
  92.     #generates a random math problem
  93.     def random_question(self):
  94.         func = ["plus", "minus", "times",]
  95.         x = random.randint(1, 99)
  96.         y = random.randint(1, 99)
  97.         function = random.choice(func)
  98.    
  99.         if function == "plus":
  100.             self.correct_answer = str(x + y)
  101.         elif function == "minus":
  102.             self.correct_answer = str(x - y)
  103.         elif function == "times":
  104.             self.correct_answer = str(x * y)
  105.        
  106.         self.question['text'] = "%d %s %d" % (x, function, y)
  107.  
  108.     def test_answer(self):
  109.         self.question_count = self.question_count+1
  110.         answer = self.display.get()
  111.        
  112.         if self.correct_answer == answer:
  113.             self.info['text'] = "Correct! (%d)" % self.question_count
  114.             self.score = self.score+1
  115.         else:
  116.             self.info['text'] = "Incorrect (%d)" % self.question_count
  117.            
  118.         if self.question_count < 10:
  119.             self.random_question()
  120.         else:
  121.             self.question['text'] = "Test over"
  122.             self.info['text'] = "You got %d out of 10" % score
  123.  
  124. #    def test(self):
  125. #        while question_count < 10:
  126. #            random_question()
  127. #        while question_count == 10:
  128. #            print("Test over")        
  129. #            print("You got", score, "out of 10,", name)
  130. #            question_count = 11
  131.            
  132. root = Tk()
  133. root.title("Calculator")
  134. root.resizable(0, 0)
  135.          
  136. Application(root).pack()
  137.  
  138. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement