Calculator

calculator

Project-2: calculator

setting up the dimension for the window:

window= tkinter.Tk()
window.title('Calculator')
window.geometry('550x500')
frame = tkinter.Frame()
window.resizable(False, False)

display/blank frame to show the result and inputs:

blank= tkinter.Label(window, height=2, bg= '#FFFFFF',fg="#000000",text='', font= ('arial', 30, "bold"), width=35 )
blank.pack()

widgets/buttons for calculator for the first row:

button_c = tkinter.Button(frame, text='C', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_c.grid(row=1,column=0)
button_div = tkinter.Button(frame, text='/', font=('arial', 30,"bold"), bd= 1, width=6, height=2, pady=4)
button_div.grid(row=1,column=1)
button_percent = tkinter.Button(frame, text='%', font=('arial', 30, "bold"), bd= 1, width=6, height=2)
button_percent.grid(row=1,column=2)
button_star = tkinter.Button(frame, text='*', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_star.grid(row=1,column=3)

widgets/buttons for calculator for the second row:

button_seven = tkinter.Button(frame, text='7', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4))
button_seven.grid(row=2,column=0)
button_eight = tkinter.Button(frame, text='8', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_eight.grid(row=2,column=1)
button_nine = tkinter.Button(frame, text='9', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_nine.grid(row=2,column=2)
button_minus = tkinter.Button(frame, text='-', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_minus.grid(row=2,column=3)

similarly for other rows:

button_four = tkinter.Button(frame, text='4', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_four.grid(row=3,column=0)
button_five = tkinter.Button(frame, text='5', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_five.grid(row=3,column=1)
button_six = tkinter.Button(frame, text='6', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_six.grid(row=3,column=2)
button_plus = tkinter.Button(frame, text='+', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_plus.grid(row=3,column=3)

button_one = tkinter.Button(frame, text='1', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_one.grid(row=4,column=0)
button_two = tkinter.Button(frame, text='2', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_two.grid(row=4,column=1)
button_three = tkinter.Button(frame, text='3', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4)
button_three.grid(row=4,column=2)
button_equal = tkinter.Button(frame, text='=', font=('arial', 30, "bold"), bd= 1, pady=4,width=6, height=4)
button_equal.grid(row=4,column=3, rowspan=2)

button_zero = tkinter.Button(frame, text='0', font=('arial', 30, "bold"), bd= 1, pady=4, width=6, height=2)
button_zero.grid(row=5,column=0)
button_dot = tkinter.Button(frame, text='.', font=('arial', 30, "bold"), bd= 1, pady=4, width=14, height=2)
button_dot.grid(row=5,column=1, columnspan=2)

created a global variables for the result and equation and define their initial values:

equation=""
result= 0

function to get the value of each button:

def show_input(input): 
    global equation
    equation+=input
    blank.config(text=equation)

using blank.config() to change the value to our display frame

creating a function to clear up the values:

def clear():
    global equation
    equation= ''
    result = 0
    blank.config(text=equation)

setting the equation value to empty string and the final result value to 0

function on = button to calculate the equation:

def result():
    global equation
    global result
    if equation != "":
      try:
          result = eval(equation)
          equation = str(result)
      except:
          result= "error"
          equation = ''
    blank.config(text=result)

calling function on button and getting arguments as an operator and operands:

command=lambda:show_input('/') 
command=lambda: clear()
command=result
# adding this in the end of each button and change argument according to the button
# and the value we want to pass.
#like:
button_c = tkinter.Button(frame, text='C', font=('arial', 30, "bold"), bd= 1, width=6, height=2, pady=4, command=lambda: clear())
button_div = tkinter.Button(frame, text='/', font=('arial', 30,"bold"), bd= 1, width=6, height=2, pady=4, command=lambda:show_input('/'))
button_equal = tkinter.Button(frame, text='=', font=('arial', 30, "bold"), bd= 1, pady=4,width=6, height=4, command=result)

using .pack to put the widgets in the window and .mainloop to start the loop:

frame.pack()
window.mainloop()