__author__ = "점수잘조"
from tkinter import *
from tkinter.filedialog import askopenfilename, asksaveasfile
import math
 # import pi, e, sin, cos, tan, log, log10, exp, sqrt
import re


class calculator():
	def __init__(self):
		def ABOUT():
			filewin = Toplevel(display)
			button = Button(filewin, text = "점수잘조 계산기")
			button.pack()
		self.error = False
		display = Tk()
		display.configure(background = 'white')
		display.title("점수잘조 계산기")
		self.string = StringVar()
		entry = Entry(display, textvariable = self.string, font = "NanumGothicExtraBold")
		entry.grid(row = 0, column = 0, columnspan = 6)
		entry.bind('<KeyPress>', self.keyPress)
		entry.focus()

		values = ["1", "2", "3", "/","Clear","<-","**",
				  "4", "5", "6", "*", "(", ")", "= f(x)",
				  "7", "8", "9", "-", "sin","f'(x)","=",
				  ".", "0", "%", "+", "cos","x" ]

		i = 0
		row = 1
		col = 0

		menubar = Menu(display)
		filemenu = Menu(menubar, tearoff = 0)
		filemenu.add_command(label = "ABOUT", command = ABOUT)
		# filemenu.add_separator()
		menubar.add_cascade(label = "Info", menu = filemenu)
		display.config(menu = menubar)
		for txt in values:
			padx = 10
			pady = 15
			if i == 7:
				row = 2
				col = 0
			if i == 14:
				row = 3
				col = 0
			if i == 21:
				row = 4
				col = 0
			# text = self.display.getText()
			# resulta = math.sin(eval(text))*(180/math.pi)
			if txt == "sin":
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text = txt,
							 command = lambda:self.sin())
				btn.grid(row = row, column = col, padx = 1, pady = 1)
			elif txt == "**":
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text = txt,
							command = lambda:self.square())
				btn.grid(row = row, column = col, padx = 1, pady = 1)
			elif txt == "f'(x)":
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text = txt,
							command = lambda:self.derivative())
				btn.grid(row = row, column = col, padx = 1, pady = 1)
			elif txt == "= f(x)":
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text = txt,
							command = lambda:self.f())
				btn.grid(row = row, column = col, padx = 1, pady = 1)
			elif txt == "=":
				btn = Button(display, height = 2, width = 2, padx = 10, pady = 35, text= txt,
							 command = lambda:self.equals())
				btn.grid(row = row, column = col, columnspan = 1, rowspan = 2, padx = 1, pady = 1)
			elif txt == "cos":
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text = txt,
							 command = lambda:self.cos())
				btn.grid(row = row, column = col, padx = 1, pady = 1)

			elif(txt =="Clear"):
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text= txt,
							 command = lambda:self.clearTxt())
				btn.grid(row = row, column = col, padx = 1, pady = 1)
			elif(txt =="<-"):
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text= txt,
							 command = lambda:self.delete())
				btn.grid(row = row, column = col, padx = 1, pady = 1)
			else:
				btn = Button(display, height = 1, width = 2, padx = padx, pady = pady, text= txt,
							 command = lambda txt = txt:self.addChar(txt))
				btn.grid(row = row, column = col, padx = 1, pady = 1)

			col = col + 1
			i = i + 1

		display.mainloop()

	

	def keyPress(self, event):

		allowedValues = ["KP_0", "KP_1", "KP_2", "KP_3", "KP_4", "KP_5", "KP_6", "KP_7", "KP_8",
							"KP_9", "7", "8", "9", "KP_Divide", "slash", "4", "5", "6",
							"KP_Multiply", "parenleft", "parenright", "1", "2", "3",
							"KP_Subtract", "minus", "equal", "0", "period", "percent", "KP_Add",
							"plus", "BackSpace", "asterisk", "Right", "Left", "KP_Decimal"]

		if not self.error:

			if event.keysym in("Return", "KP_Enter"):
				self.equals()
			elif event.keysym not in allowedValues:
				return 'break'
		else:
			return 'break'
	def clearTxt(self):
		self.string.set("")
		self.error = False

	def square(self):
		self.string.set(str(self.string.get())+'**')
	def sin(self):
		result = ""
		try:
			C = math.radians(float(self.string.get()))
			result = math.sin(float(C))
			if 0 <= result <=0.0000001:
				result = 0
			elif -0.0000001<= result <= 0:
				result = 0
		except:
			self.error = True
			result = "Error"
		self.string.set(result)

	def f(self):
		return self.string.get()

	def derivative(self):
		h = 1./1000.
		rise = self.string.get + h
		run = h
		result = rise/run

		self.string.set(result)
	def cos(self):
		result = ""
		try:
			C = math.radians(float(self.string.get()))
			result = math.cos(float(C))
			if 0 <= result <=0.0000001:
				result = 0
			elif -0.0000001<= result <= 0:
				result = 0
		except:
			self.error = True
			result = "Error"
		self.string.set(result)

	def equals(self):
		result = ""
		try:
			result = eval(self.string.get())
		except:
			self.error = True
			result = "Error"
		self.string.set(result)

	def addChar(self, char):
		if not (self.error):
			self.string.set(self.string.get() + (str(char)))

	def delete(self):
		if not(self.error):
			self.string.set(self.string.get()[0:-1])

calculator()
