Sunteți pe pagina 1din 4

from tkinter import *

import math

operation = "N"
stored_value = 0
flag = 0
new_value = 0

results = []

def getStoredVal():
global stored_value
try:
if (stored_value == "pi"):
stored_value = math.pi()
elif (stored_value == "e"):
stored_value = math.e()
else:
float(stored_value)
return stored_value
except:
stored_value = 0
float(stored_value)
return stored_value

def reset(sum):
global operation
text4.insert('0.0', sum)
entry_left.delete(0, END)
results.append(sum)
operation = "N"

def clrBtn():
global operation
entry_left.delete(0, END)
operation = "N"

def divisionCheck(stored_value, new_value, typeOf):


try:
if typeOf == "D":
sum = float(stored_value) / float(new_value)
if typeOf == "MO":
sum = float(stored_value) % float(new_value)
if typeOf == "ID":
sum = float(stored_value) // float(new_value)
return sum
except:
print("An error occured: do not divide by zero")
entry_left.delete(0, END)
return

def btn(operation_name):
global stored_value
global flag
global operation
operation = operation_name
if flag == 0:
stored_value = entry_left.get()
entry_left.delete(0, END)
flag = 1
if flag == 1:
flag = 0

def calculate():
stored_value = getStoredVal()
global operation
global new_value

if operation in ("A", "S", "M", "D", "ID", "MO", "EX", "RO", "LOG", "ABS",
"FAC", "SIN", "COS", "TAN"):
text4.delete('0.0', END)
new_value = entry_left.get()
if operation == "A":
sum = float(stored_value) + float(new_value)
reset(sum)
elif operation == "S":
sum = float(stored_value) - float(new_value)
reset(sum)
elif operation == "M":
sum = float(stored_value) * float(new_value)
reset(sum)
elif operation == "D":
sum = divisionCheck(stored_value, new_value, operation)
reset(sum)
elif operation == "ID":
sum = divisionCheck(stored_value, new_value, operation)
reset(sum)
elif operation == "MO":
sum = divisionCheck(stored_value, new_value, operation)
reset(sum)
elif operation == "EX":
sum = float(stored_value) ** float(new_value)
reset(sum)
elif operation == "RO":
sum = float(new_value) ** (1 / float(stored_value))
# first input is the root type
reset(sum)
elif operation == "LOG":
sum = math.log(float(new_value),float(stored_value))
# first input is the base
reset(sum)
elif operation == "ABS":
sum = math.fabs(float(stored_value))
reset(sum)
elif operation == "FAC":
sum = math.factorial(float(stored_value))
reset(sum)
elif operation == "SIN":
sum = math.sin(math.radians(int(new_value)))
reset(sum)
elif operation == "COS":
sum = math.cos(math.radians(float(new_value)))
reset(sum)
elif operation == "TAN":
sum = math.tan(math.radians(float(new_value)))
reset(sum)
print("History: ", results)
else:
return

def addBtn():
btn("A")

def multBtn():
btn("M")

def subBtn():
btn("S")

def divBtn():
btn("D")

def intDivBtn():
btn("ID")

def modBtn():
btn("MO")

def expBtn():
btn("EX")

def rootBtn():
btn("RO")

def logBtn():
btn("LOG")

def absBtn():
btn("ABS")

def factBtn():
btn("FAC")

def sinBtn():
btn("SIN")

def cosBtn():
btn("COS")

def tanBtn():
btn("TAN")

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

text1 = Label(window, width=10, height=1, text='Number Input:', bg="linen")


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

entry_left = Entry(window, width=10)


entry_left.grid(row=1, column=0, ipadx=4, sticky=W+E)

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=4, height=4, text='%', command=modBtn)
button5.grid(row=5, column=0, ipadx=2, sticky=W)
button6 = Button(window, width=4, height=4, text='//', command=intDivBtn)
button6.grid(row=5, column=0, sticky=E)
button7 = Button(window, width=4, height=4, text=' ^ ', command=expBtn)
button7.grid(row=6, column=0, sticky=W)
button8 = Button(window, width=4, height=4, text='v', command=rootBtn)
button8.grid(row=6, column=0, sticky=E)
button9 = Button(window, width=4, height=4, text='log', command=logBtn)
button9.grid(row=7, column=0, sticky=W)
button10 = Button(window, width=4, height=4, text='ABS', command=absBtn)
button10.grid(row=7, column=0, sticky=E)
button11 = Button(window, width=4, height=4, text='!', command=factBtn)
button11.grid(row=8, column=0, sticky=W)
button12 = Button(window, width=4, height=4, text='sin', command=sinBtn)
button12.grid(row=8, column=0, sticky=E)
button13 = Button(window, width=4, height=4, text='cos', command=cosBtn)
button13.grid(row=9, column=0, sticky=W)
button14 = Button(window, width=4, height=4, text='tan', command=tanBtn)
button14.grid(row=9, column=0, sticky=E)
button15 = Button(window, width=10, height=2, text='=', command=calculate)
button15.grid(row=10, column=0, sticky=W+E)
button16 = Button(window, width=10, height=2, text='CLR', command=clrBtn)
button16.grid(row=11, column=0, sticky=W+E)

window.configure(background="linen")
window.mainloop()

S-ar putea să vă placă și