diff --git a/Problem Statement 1/README.md b/Problem Statement 1/README.md deleted file mode 100644 index 6aaeab6..0000000 --- a/Problem Statement 1/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Problem Statement-1. - - -*** -### Make a desktop assistant using Python as its backend - - - -*** - diff --git a/Problem Statement 2/README.md b/Problem Statement 2/README.md deleted file mode 100644 index baf2489..0000000 --- a/Problem Statement 2/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Problem Statement-2. - - -*** -### Make a Simple Image Editing app - - - -*** - diff --git a/Problem Statement 3/README.md b/Problem Statement 3/README.md index b4683b3..bf60d4f 100644 --- a/Problem Statement 3/README.md +++ b/Problem Statement 3/README.md @@ -1,9 +1,32 @@ +Link for task submission video +TASK 3 CALCULATOR Using Python : +**https://youtu.be/1OeMWxhRSOA** + + # Problem Statement-3. *** ### Make a calculator with GUI +Hello +I’m Chetan Chinchulkar from IITG + +This video is regarding problem statement 3 under GameOfCodes. GameOfCodes Python is a coding competition based on Python problem statements. +Problem statement 3 is Calculator with GUI using Python + +There are two versions of the calculator : +Standard calculator +Bsic mathemcatival operations are available +Scientific calculator +Advanced mathematical operations like trigometric functions,exponential etc are added + +In addition to this, it has Rand button to randomly generate and show any random number. This also has Signum function. + +Error Handling +The GUI takes care of varies error by correspondingly displaying error messages. + +Uniqueness of this calculator is copying the result, and also has age calculator *** diff --git a/Problem Statement 3/__pycache__/age_calculator.cpython-38.pyc b/Problem Statement 3/__pycache__/age_calculator.cpython-38.pyc new file mode 100644 index 0000000..9c1379e Binary files /dev/null and b/Problem Statement 3/__pycache__/age_calculator.cpython-38.pyc differ diff --git a/Problem Statement 3/age.ico b/Problem Statement 3/age.ico new file mode 100644 index 0000000..e5f7386 Binary files /dev/null and b/Problem Statement 3/age.ico differ diff --git a/Problem Statement 3/age_calculator.py b/Problem Statement 3/age_calculator.py new file mode 100644 index 0000000..654fc8c --- /dev/null +++ b/Problem Statement 3/age_calculator.py @@ -0,0 +1,104 @@ +# importing modules +from email import message +from tkinter import * +from datetime import date,datetime +from tkinter import messagebox + + +root = Tk() # creating window +root.title("AGE-CALCULATOR") # setting up title +root.configure(bg="#fff") # setting up background color +root.geometry("400x300") # fixing the size of the window +root.iconbitmap("age.ico") # set Icon +new = Label(root, bg="#fff") # declaring a label +new.grid(row=5, column=0, columnspan=3) + +today = str(date.today()) # getting current date using datetime module +list_today = today.split("-") # converting into a list + + +# defining a function to calculate age +def age(b_date, b_month, b_year): + try: + global today + global new + + + date_string = entry_year.get()+'-'+entry_month.get()+'-'+entry_date.get() + birthdate = datetime.strptime(date_string, '%Y-%m-%d').date() + + if birthdate>date.today(): + messagebox.showwarning("WARNING", "You are not born yet \n Enter correct date") + else: + new.grid_forget() + b_date = int(entry_date.get()) + b_month = int(entry_month.get()) + b_year = int(entry_year.get()) + c_date = int(list_today[2]) + c_month = int(list_today[1]) + c_year = int(list_today[0]) + month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + if b_date > c_date: + c_month = c_month-1 + c_date = c_date+month[b_month-1] + if b_month > c_month: + c_year = c_year-1 + c_month = c_month+12 + resultd = str(c_date-b_date) + resultm = str(c_month-b_month) + resulty = str(c_year-b_year) + new = Label(root, text="YOUR AGE \n"+resulty+" YEARS "+resultm+" MONTHS " + + resultd+" DAYS ", fg="#000", bg="#D5C6FF", borderwidth=6) + new.config(font=("Arial Rounded MT Bold", 15)) + new.grid(row=5, column=0, columnspan=3) + except Exception as err: + messagebox.showerror("Error", "Please enter a valid date \n Error : "+str(err)) + + +# defining a function to clear the previous entries +def clean(entry_date, entry_month, entry_year): + global new + new.grid_forget() + entry_date.delete(0, END) + entry_month.delete(0, END) + entry_year.delete(0, END) + + +# creating widgets such as labels,entry boxes and buttons and fixing its position onto window +title_label = Label(root, text="AGE CALCULATOR", borderwidth=10, fg="#404042", bg="#fff") +title_label.config(font=("Arial Rounded MT Bold", 29)) +title_label.grid(row=0, column=0, columnspan=3) +label_date = Label(root, text="BIRTH DATE : ", borderwidth=4, fg="#404042", bg="#fff") +label_date.config(font=("Arial Rounded MT Bold", 15)) +label_date.grid(row=1, column=0) +label_month = Label(root, text="BIRTH MONTH : ", borderwidth=5, fg="#404042", bg="#fff") +label_month.config(font=("Arial Rounded MT Bold", 15)) +label_month.grid(row=2, column=0) +label_year = Label(root, text="BIRTH YEAR : ", borderwidth=9, fg="#404042", bg="#fff") +label_year.config(font=("Arial Rounded MT Bold", 15)) +label_year.grid(row=3, column=0) + +entry_date = Entry(root, width=20, borderwidth=3) +entry_month = Entry(root, width=20, borderwidth=3) +entry_year = Entry(root, width=20, borderwidth=3) + +entry_date.grid(row=1, column=2) +entry_month.grid(row=2, column=2) +entry_year.grid(row=3, column=2) + +# getting the value in the entry boxes +b_date = entry_date.get() +b_month = entry_month.get() +b_year = entry_year.get() + +# calling age function in button widget +submit = Button(root, text="GET AGE!!", width=10, anchor=CENTER, command=lambda: age(b_date, b_month, b_year), + fg="black" ) +submit.grid(row=4, column=0) + +# calling clean function in button widget +clear = Button(root, text="CLEAR", width=10, command=lambda: clean(entry_date, entry_month, entry_year), + fg="black") +clear.grid(row=4, column=2) + +root.mainloop() diff --git a/Problem Statement 3/images/logo1.png b/Problem Statement 3/images/logo1.png new file mode 100644 index 0000000..a3e0ada Binary files /dev/null and b/Problem Statement 3/images/logo1.png differ diff --git a/Problem Statement 3/images/logo2.ico b/Problem Statement 3/images/logo2.ico new file mode 100644 index 0000000..c5dce77 Binary files /dev/null and b/Problem Statement 3/images/logo2.ico differ diff --git a/Problem Statement 3/images/logo2.png b/Problem Statement 3/images/logo2.png new file mode 100644 index 0000000..40b1454 Binary files /dev/null and b/Problem Statement 3/images/logo2.png differ diff --git a/Problem Statement 3/main_calc.py b/Problem Statement 3/main_calc.py new file mode 100644 index 0000000..bc0cfad --- /dev/null +++ b/Problem Statement 3/main_calc.py @@ -0,0 +1,372 @@ +from ast import Try +from cgitb import text +from tkinter import * +from tkinter import messagebox +from math import pi, e, sin, cos, tan, log +import tkinter.font +import random +import math +from datetime import date + + + + + + +window = Tk() + +window.geometry("722x400") +window.resizable(0, 0) +window.title("Calcualtor") + +icon = PhotoImage(file="./images/logo2.png") + +window.iconphoto(False, icon) +window.iconbitmap("./images/logo2.ico") + + + +cal = Frame(window, width=300, height=500) +cal.grid() +cal.config(bg='#f0f0f0') +equation = StringVar() + +# Area to input the equation +input_area = Entry(cal, textvariable=equation, width=60, font=("Comic Sans MS", 15), bd=10, justify=LEFT, state=DISABLED, + disabledbackground="white", disabledforeground="black") +input_area.insert(0, "0") +input_area.grid(row=0, columnspan=8) + +font = tkinter.font.Font(size=12, weight="bold", family='Helvetica',) + + +def about(): + messagebox.showinfo( + 'About', "\n \n \n Made by : Chetan Chinchulkar \n Institute : IIT Guwahati \n Branch : Engineering Physics \n \n") + + +def standard(): + window.geometry('361x400') + input_area['width'] = 28 + input_area.grid(row=0, columnspan=4, sticky=EW) + window.title("Standard Calculator") + +def scientific(): + window.geometry('722x400') + input_area['width'] = 60 + input_area.grid(row=0, columnspan=8) + window.title("Scientific Calculator") + +def sgn(a): + return 1 if a>0 else -1 if a<0 else 0 + +def age_cal(): + import age_calculator + +new_menu = Menu(window) +filemenu = Menu(new_menu, tearoff=0) +new_menu.add_cascade(label="File", menu=filemenu) +filemenu.add_command(label="Standard", command=standard) +filemenu.add_command(label="Scientific", command=scientific) +filemenu.add_separator() +filemenu.add_command(label="Age Calculator", command=age_cal) +filemenu.add_separator() +filemenu.add_command(label="About", command=about) +filemenu.add_separator() +filemenu.add_command(label="Exit", command=quit) +window.config(menu=new_menu) + + +value = "" +ans = "" +expression = "" +history = {} + + +h = 2 +w = 7 +actvbgnd = 'white' +bg1 = 'wheat3' +bg2 = "burlywood1" +bg3 = "burlywood2" +bg4 = "tan1" +fg1 = "white" +fg2 = "black" + +numberpad = [7, 8, 9, 4, 5, 6, 1, 2, 3] + +class Application(Frame): + def __init__(self, master, *args, **kwargs): + Frame.__init__(self, master, *args, **kwargs) + self.createWidgets() + + def clearall(self): + global expression + global equation + global value + global ans + expression='' + value='' + ans='' + equation.set(expression) + + def clearback(self): + result1="" + result2="" + global equation + global expression + global value + global ans + + expression = input_area.get() + temp1= list(expression) + temp2= list(value) + + if value=='': + temp1=[] + temp2=[] + elif expression[-5:] in ["asin(","acos(","atan("]: + for _ in range(5):temp1.pop() + for _ in range(10):temp2.pop() + + elif expression[-4:]=="log(": + for _ in range(4):temp1.pop() + for _ in range(11):temp2.pop() + + elif expression[-4:] in ['sin(','cos(','tan(']: + for _ in range(4): temp1.pop() + for _ in range(9): temp2.pop() + + elif expression[-4:]=='sgn(': + for _ in range(4): temp1.pop() + for _ in range(4): temp2.pop() + + elif expression[-3:]=='ln(': + for _ in range(3):temp1.pop() + for _ in range(9): temp2.pop() + + elif expression[-2:]=='e^': + for _ in range(2):temp1.pop() + for _ in range(8): temp2.pop() + + elif expression[-1]=='^': + for _ in range(1):temp1.pop() + for _ in range(2): temp2.pop() + + elif expression[-1]=="√": + for _ in range(1):temp1.pop() + for _ in range(10):temp2.pop() + + elif expression[-1]=='π': + for _ in range(1):temp1.pop() + for _ in range(7): temp2.pop() + + elif expression[-1]=='e': + for _ in range(1):temp1.pop() + for _ in range(6): temp2.pop() + + elif expression[-1]=='%': + for _ in range(1):temp1.pop() + for _ in range(4): temp2.pop() + + else: + temp1.pop() + temp2.pop() + + for element in range(len(temp1)): + result1+=temp1[element] + expression = result1 + equation.set(expression) + + for element in range(len(temp2)): + result2+=temp2[element] + + value=result2 + try:ans = str(eval(value)) + except:pass + + def pressbtn(self,num): + global expression + global value + global ans + expression = expression + str(num) + equation.set(expression) + if num in ["1","2","3","4","5","6","7","8","9","0","(",")","00"]: + value += num + try:ans = str(eval(value)) + except:ans = "Invalid Expression" + + elif num in ["+",'-','/','*','.','1/','sgn(']: + value += num + + elif num in ['asin(','acos(','atan(','sin(','cos(','tan(']: + value += 'math.'+ num + + elif num=='^':value += '**' + + elif num=='%': + value += '/100' + try:ans = str(eval(value)) + except:ans = "Invalid Expression" + elif num=='^2': + value += '**2' + try:ans = str(eval(value)) + except:ans = "Invalid Expression" + elif num=='^3': + value += '**3' + try:ans = str(eval(value)) + except:ans = "Invalid Expression" + + elif num=='√(':value += 'math.sqrt(' + + elif num=='e': + value += 'math.e' + try:ans = str(eval(value)) + except:ans = "Invalid Expression" + elif num=='π': + value += 'math.pi' + try:ans = str(eval(value)) + except:ans = "Invalid Expression" + elif num=='log(':value += 'math.log10(' + elif num=='ln(':value += 'math.log(' + elif num=='e^':value += 'math.e**' + + + def equal_calc(self): + global expression + global ans + global value + + # print(expression) + # print(ans) + # print(value) + + if expression=='': + messagebox.showerror("Error", "Please enter an expression") + else: + try: + ans = str(eval(value)) + equation.set(ans) + history[expression] = ans + + # print(result) + messagebox.showinfo("Result", expression + " = " + str(ans)) + ans='' + value='' + expression='' + except Exception as err: + + # messagebox.showerror("Calculation Error", err) + messagebox.showerror("Calculation Error", "ERROR...") + equation.set("ERROR...") + + def copy_res(self): + global expression + + if expression=='': + messagebox.showerror("Error", "Please enter an expression") + else: + import subprocess + subprocess.run("pbcopy",universal_newlines=True, input=input_area.get()) + messagebox.showinfo("Copied", "Result copied to clipboard") + + def rand(self): + # input_area.config(text= random.random()) + global ans + global expression + global value + + ans = (random.random()) + expression = str(ans) + value = str(ans) + equation.set(ans) + + def check_history(self): + history_text = [] + for key,value in history.items(): + history_text.append(key + " : " + value) + + if history_text==[]: + messagebox.showinfo("History", "No calculations done yet") + else: + messagebox.showinfo("History", "\n".join(history_text)) + + def createWidgets(self): + # self.display = Entry(self, font=("Helvetica", 16), borderwidth=0, relief=RAISED, justify=RIGHT) + # self.display.insert(0, "0") + # self.display.grid(row=0, column=0, columnspan=5) + + i=0 + for j in range(3): + for k in range(3): + Button(cal,command = lambda x = str(numberpad[i]) : self.pressbtn(x), text = str(numberpad[i]), bg= bg1, fg=fg2,activebackground=actvbgnd, + height=h, width=w,font= font).grid(row=j+2,column=k) + i+=1 + + r=5 + c=7 + Button(cal,command = lambda: self.pressbtn('0'), text = "0", bg= bg1, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r,column= c-7) + + Button(cal,command = lambda: self.pressbtn('00'),text = "00", bg= bg1, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r,column= c-6) + + Button(cal,command = self.clearback, text = "C", bg= bg2, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r-4,column= c-7) + + Button(cal,command = self.clearall, text = "AC",bg= bg2, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r-4,column= c-6) + + Button(cal,command = lambda: self.pressbtn('.'), text = "•", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r,column=c-5) + + Button(cal,command = lambda: self.pressbtn('+'), text = "+", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r-2,column=c-4) + + Button(cal,command = lambda: self.pressbtn('-'), text = "–", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r-3,column=c-4) + + Button(cal,command = lambda: self.pressbtn('/'), text = "/", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r-4,column=c-5) + + Button(cal,command = lambda: self.pressbtn('*'), text = "✶", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r-4,column=c-4) + + Button(cal,command = self.equal_calc, text = "=", bg= bg2, fg=fg2, activebackground = actvbgnd, + height=3*h,width=w,font= font,pady=10).grid(row=r-1,column=c-4,rowspan=2,) + + Button(cal,command = self.rand, text = "Rand", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w,font= font).grid(row=r+1,column=c-7) + + Button(cal,command = self.copy_res, text = "Copy Result", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=w ,font= font).grid(row=r+1,column=c-6) + + Button(cal,command = self.check_history, text = "Check History", bg= bg3, fg=fg2, activebackground = actvbgnd, + height=h, width=2*w ,font= font).grid(row=r+1,column=c-5,columnspan=2) + + + + list1=['(',')','%','asin','sin','log','x^2','acos','cos','ln','x^3','atan','tan','e^x','1/x','x^y','e',"π",'√x','sgn'] + list2=['(',')','%','asin(','sin(','log(','^2','acos(','cos(','ln(','^3','atan(','tan(','e^','1/','^','e',"π",'√(','sgn('] + i=0 + for j in range(5): + for k in range(4): + Button(cal,command = lambda x= list2[i]: self.pressbtn(x), text = list1[i], bg=bg4, fg= fg2,activebackground=actvbgnd, + height=h,width=w,font= font).grid(row=j+1,column=k+4) + i+=1 + + msize=60 + cal.rowconfigure(0,minsize=50) + for i in range(1,6): + cal.rowconfigure(i,minsize=60) + + msize = 90 + for i in range(8): + cal.columnconfigure(i,minsize= msize) + + + + +app = Application(window) + +window.mainloop() diff --git a/Problem Statement 3/requirements.txt b/Problem Statement 3/requirements.txt new file mode 100644 index 0000000..a70021f --- /dev/null +++ b/Problem Statement 3/requirements.txt @@ -0,0 +1,4 @@ +autopep8==1.6.0 +pycodestyle==2.8.0 +pyglet==1.5.26 +toml==0.10.2 diff --git a/README.md b/README.md index dd00367..48eee3f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +Link for task submission video +TASK 3 CALCULATOR Using Python : +**https://youtu.be/1OeMWxhRSOA** + # GAME OF CODES