Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- class Application(Frame):
- def __init__(self, master, *args, **kwargs):
- Frame.__init__(self, master, *args, **kwargs)
- self.score = 0
- self.question_count = 0
- self.createWidgets()
- self.random_question()
- def replaceText(self, text):
- self.display.delete(0, END)
- self.display.insert(0, text)
- def calculateExpression(self):
- self.expression = self.display.get()
- try:
- self.result = eval(self.expression)
- self.replaceText(self.result)
- except:
- messagebox.showinfo("Error", "Invalid Input")
- def appendToDisplay(self, text):
- self.entryText = self.display.get()
- self.textLength = len(self.entryText)
- if self.entryText == "0":
- self.replaceText(text)
- else:
- self.display.insert(self.textLength, text)
- def clearText(self):
- self.replaceText("0")
- def createWidgets(self):
- self.display = Entry(self, font = ("Calibri", 16), borderwidth = 1, relief = RAISED, justify = RIGHT)
- self.display.insert(0, "0")
- self.display.grid(row = 0, column = 0, columnspan = 4)
- self.sevenButton = Button(self, font = ("Calibri", 15), text = "7", borderwidth = 1, command = lambda: self.appendToDisplay("7"))
- self.sevenButton.grid(row=1, column=0, sticky="NWNESWSE")
- self.eightButton = Button(self, font = ("Calibri", 15), text = "8", borderwidth = 1, command = lambda: self.appendToDisplay("8"))
- self.eightButton.grid(row=1, column=1, sticky="NWNESWSE")
- self.nineButton = Button(self, font = ("Calibri", 15), text = "9", borderwidth = 1, command = lambda: self.appendToDisplay("9"))
- self.nineButton.grid(row=1, column=2, sticky="NWNESWSE")
- self.clearButton = Button(self, font=("Calibri", 15), text = "C", borderwidth = 1, command = lambda: self.clearText())
- self.clearButton.grid(row = 1, rowspan = 2, column = 3, sticky = "NWNESWSE")
- self.fourButton = Button(self, font = ("Calibri", 15), text = "4", borderwidth = 1, command = lambda: self.appendToDisplay("4"))
- self.fourButton.grid(row = 2, column = 0, sticky = "NWNESWSE")
- self.fiveButton = Button(self, font = ("Calibri", 15), text = "5", borderwidth = 1, command = lambda: self.appendToDisplay("5"))
- self.fiveButton.grid(row = 2, column = 1, sticky = "NWNESWSE")
- self.sixButton = Button(self, font = ("Calibri", 15), text = "6", borderwidth = 1, command = lambda: self.appendToDisplay("6"))
- self.sixButton.grid(row = 2, column = 2, sticky = "NWNESWSE")
- self.oneButton = Button(self, font = ("Calibri", 15), text = "1", borderwidth = 1, command = lambda: self.appendToDisplay("1"))
- self.oneButton.grid(row = 3, column = 0, sticky = "NWNESWSE")
- self.twoButton = Button(self, font = ("Calibri", 15), text = "2", borderwidth = 1, command = lambda: self.appendToDisplay("2"))
- self.twoButton.grid(row = 3, column = 1, sticky = "NWNESWSE")
- self.threeButton = Button(self, font = ("Calibri", 15), text = "3", borderwidth = 1, command = lambda: self.appendToDisplay("3"))
- self.threeButton.grid(row = 3, column = 2, sticky = "NWNESWSE")
- self.OKButton = Button(self, font = ("Calibri", 15), text = "OK", borderwidth = 1, command=self.test_answer)
- self.OKButton.grid(row = 3, rowspan = 2, column = 3, sticky = "NWNESWSE")
- self.zeroButton = Button(self, font = ("Calibri", 15), text = "0", borderwidth = 1, command = lambda: self.appendToDisplay("0"))
- self.zeroButton.grid(row = 4, column = 0, columnspan = 2, sticky = "NWNESWSE")
- self.dotButton = Button(self, font = ("Calibri", 15), text = ".", borderwidth = 1, command = lambda: self.appendToDisplay("."))
- self.dotButton.grid(row = 4, column = 2, sticky = "NWNESWSE")
- self.question = Label(self)
- self.question.grid(row=5, column=0, columnspan = 4)
- self.info = Label(self)
- self.info.grid(row=6, column=0, columnspan = 4)
- #generates a random math problem
- def random_question(self):
- func = ["plus", "minus", "times",]
- x = random.randint(1, 99)
- y = random.randint(1, 99)
- function = random.choice(func)
- if function == "plus":
- self.correct_answer = str(x + y)
- elif function == "minus":
- self.correct_answer = str(x - y)
- elif function == "times":
- self.correct_answer = str(x * y)
- self.question['text'] = "%d %s %d" % (x, function, y)
- def test_answer(self):
- self.question_count = self.question_count+1
- answer = self.display.get()
- if self.correct_answer == answer:
- self.info['text'] = "Correct! (%d)" % self.question_count
- self.score = self.score+1
- else:
- self.info['text'] = "Incorrect (%d)" % self.question_count
- if self.question_count < 10:
- self.random_question()
- else:
- self.question['text'] = "Test over"
- self.info['text'] = "You got %d out of 10" % score
- # def test(self):
- # while question_count < 10:
- # random_question()
- # while question_count == 10:
- # print("Test over")
- # print("You got", score, "out of 10,", name)
- # question_count = 11
- root = Tk()
- root.title("Calculator")
- root.resizable(0, 0)
- Application(root).pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement