You are on page 1of 2

from tkinter import *

operation = "N"
flag = 0

def mathadd(x,y):
sum = float(x) + float(y)
return sum

def subtract(x,y):
sum = float(x) - float(y)
return sum

def multiply(x,y):
sum = float(x) * float(y)
return sum

def divide(x,y):
sum = float(x) / float(y)
return sum

def calculate():
text4.delete('0.0', END)
global stored_value
global operation
if operation == "A":
sum = mathadd(stored_value,entry_left.get())
text4.insert('0.0', sum)
print(sum)
operation = "N"
if operation == "S":
sum = subtract(stored_value,entry_left.get())
text4.insert('0.0', sum)
print(sum)
operation = "N"
if operation == "M":
sum = multiply(stored_value,entry_left.get())
text4.insert('0.0', sum)
print(sum)
operation = "N"
if operation == "D":
sum = divide(stored_value,entry_left.get())
text4.insert('0.0', sum)
print(sum)
operation = "N"

def addBtn():
global operation
global flag
operation = "A"
if flag == 0:
stored_value = entry_left.get()
flag = 1
print("Operation is now A!")

def multBtn():
global operation
global flag
operation = "M"
if flag == 0:
stored_value = entry_left.get()
flag = 1
print("Operation is now W!")

def subBtn():
global operation
global flag
operation = "S"
if flag == 0:
stored_value = entry_left.get()
flag = 1
print("Operation is now S!")

def divBtn():
global operation
global flag
operation = "D"
if flag == 0:
stored_value = entry_left.get()
flag = 1
print("Operation is now D!")

window = Tk()
window.title('Calculator')

text1 = Label(window, width=8, height=1, text='Number 1:')


text1.grid(row=0, column=0, sticky=W)
text3 = Label(window, width=8, height=1, text='Answer:')
text3.grid(row=3, column=1, sticky=W)
text4 = Text(window, width=7, height=1)
text4.grid(row=4, column=1, sticky=E)

entry_right = Entry(window, width=10)


entry_left = Entry(window, width=10)
entry_right.grid(row=1, column=0, ipadx=4, sticky=E)
entry_left.grid(row=1, column=0, ipadx=4, sticky=W)

button1 = Button(window, width=4, height=4, text='+', command=addBtn)


button1.grid(row=3, column=0, ipadx=2, sticky=W)
button2 = Button(window, width=4, height=4, text='-', command=subBtn)
button2.grid(row=3, column=0, sticky=E)
button3 = Button(window, width=4, height=4, text='*', command=multBtn)
button3.grid(row=4, column=0, ipadx=2, sticky=W)
button4 = Button(window, width=4, height=4, text='/', command=divBtn)
button4.grid(row=4, column=0, sticky=E)
button5 = Button(window, width=10, height=2, text='=', command=calculate)
button5.grid(row=5, column=0, sticky=W+E)

window.mainloop()

You might also like