from Tkinter import * import parser root = Tk() root.title('Calculator') i = 0 def clear_all(): """clears all the content in the Entry widget""" display.delete(0, END) def get_variables(num): """Gets the user input for operands and puts it inside the entry widget""" global i display.insert(i, num) i += 1 def get_operation(operator): """Gets the operand the user wants to apply on the functions""" global i length = len(operator) display.insert(i, operator) i += length def calculate(): """Evaluates the expression""" whole_string = display.get() try: formula = parser.expr(whole_string).compile() result = eval(formula) clear_all() display.insert(0, result) except Exception: clear_all() display.insert(0, "Error!") root.columnconfigure(0,pad=3) root.columnconfigure(1,pad=3) root.columnconfigure(2,pad=3) root.columnconfigure(3,pad=3) root.columnconfigure(4,pad=3) root.rowconfigure(0,pad=3) root.rowconfigure(1,pad=3) root.rowconfigure(2,pad=3) root.rowconfigure(3,pad=3) display = Entry(root, font = ("Calibri", 20)) display.grid(row = 1, columnspan = 6, sticky = W+E) one = Button(root, text = "1", command = lambda : get_variables(1), font=("Calibri", 20)) one.grid(row = 2, column = 0) two = Button(root, text = "2", command = lambda : get_variables(2), font=("Calibri", 20)) two.grid(row = 2, column = 1) three = Button(root, text = "3", command = lambda : get_variables(3), font=("Calibri", 20)) three.grid(row = 2, column = 2) four = Button(root, text = "4", command = lambda : get_variables(4), font=("Calibri", 20)) four.grid(row = 3 , column = 0) five = Button(root, text = "5", command = lambda : get_variables(5), font=("Calibri", 20)) five.grid(row = 3, column = 1) six = Button(root, text = "6", command = lambda : get_variables(6), font=("Calibri", 20)) six.grid(row = 3, column = 2) seven = Button(root, text = "7", command = lambda : get_variables(7), font=("Calibri", 20)) seven.grid(row = 4, column = 0) eight = Button(root, text = "8", command = lambda : get_variables(8), font=("Calibri", 20)) eight.grid(row = 4, column = 1) nine = Button(root , text = "9", command = lambda : get_variables(9), font=("Calibri", 20)) nine.grid(row = 4, column = 2) zero = Button(root, text = "0", command = lambda: get_variables(0), font=("Calibri", 20)) zero.grid(row = 5, column = 0) cls = Button(root, text = "Clear", command = clear_all, font=("Calibri", 20)) cls.grid(row = 5, column = 1) result = Button(root, text = "=", command = calculate, font=("Calibri", 20)) result.grid(row = 5, column = 2) plus = Button(root, text = "+", command = lambda : get_operation("+"), font=("Calibri", 20)) plus.grid(row = 2, column = 3) minus = Button(root, text = "-", command = lambda : get_operation("-"), font=("Calibri", 20)) minus.grid(row = 3, column = 3) multiply = Button(root,text = "*", command = lambda : get_operation("*"), font=("Calibri", 20)) multiply.grid(row = 4, column = 3) divide = Button(root, text = "/", command = lambda : get_operation("/"), font=("Calibri", 20)) divide.grid(row = 5, column = 3) root.mainloop()