From 7174659c9ba25daaaf2030ae77b9a7ec9d93de03 Mon Sep 17 00:00:00 2001 From: John Paul Date: Sat, 6 Jan 2024 19:31:49 +0530 Subject: [PATCH] Update My cal.py with Refactored and Enhanced Calculator Code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cleaned up and organized the calculator code for better readability and maintainability. Added functionality for square root (√), percentage (%), and exponentiation (^). Replaced duplicate functions for the equal button with a single function (calculate_result). Improved variable names and structure to enhance code clarity. Applied consistent formatting throughout the code. --- My cal.py | 148 ++++++++++++++++++++++-------------------------------- 1 file changed, 61 insertions(+), 87 deletions(-) diff --git a/My cal.py b/My cal.py index d5be8a4..ae79a92 100644 --- a/My cal.py +++ b/My cal.py @@ -1,98 +1,72 @@ -from tkinter import* - -me=Tk() +from tkinter import * + +def clickbut(number): + global operator + operator = operator + str(number) + textin.set(operator) + +def clear_entry(): + textin.set('') + +def calculate_result(): + try: + result = str(eval(operator)) + textin.set(result) + except Exception as e: + textin.set("Error") + +def add_functionality(symbol): + global operator + operator = operator + str(symbol) + textin.set(operator) + +def square_root(): + try: + result = str(eval(operator) ** 0.5) + textin.set(result) + except Exception as e: + textin.set("Error") + +def percentage(): + try: + result = str(eval(operator) / 100) + textin.set(result) + except Exception as e: + textin.set("Error") + +def exponentiation(): + global operator + operator = operator + "**" + textin.set(operator) + +me = Tk() me.geometry("354x460") me.title("CALCULATOR") -melabel = Label(me,text="CALCULATOR",bg='White',font=("Times",30,'bold')) +melabel = Label(me, text="CALCULATOR", bg='White', font=("Times", 30, 'bold')) melabel.pack(side=TOP) me.config(background='Dark gray') -textin=StringVar() -operator="" - -def clickbut(number): #lambda:clickbut(1) - global operator - operator=operator+str(number) - textin.set(operator) - -def equlbut(): - global operator - add=str(eval(operator)) - textin.set(add) - operator='' -def equlbut(): - global operator - sub=str(eval(operator)) - textin.set(sub) - operator='' -def equlbut(): - global operator - mul=str(eval(operator)) - textin.set(mul) - operator='' -def equlbut(): - global operator - div=str(eval(operator)) - textin.set(div) - operator='' +textin = StringVar() +operator = "" -def clrbut(): - textin.set('') - - -metext=Entry(me,font=("Courier New",12,'bold'),textvar=textin,width=25,bd=5,bg='powder blue') +metext = Entry(me, font=("Courier New", 12, 'bold'), textvar=textin, width=25, bd=5, bg='powder blue') metext.pack() -but1=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(1),text="1",font=("Courier New",16,'bold')) -but1.place(x=10,y=100) - -but2=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(2),text="2",font=("Courier New",16,'bold')) -but2.place(x=10,y=170) - -but3=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(3),text="3",font=("Courier New",16,'bold')) -but3.place(x=10,y=240) - -but4=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(4),text="4",font=("Courier New",16,'bold')) -but4.place(x=75,y=100) - -but5=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(5),text="5",font=("Courier New",16,'bold')) -but5.place(x=75,y=170) - -but6=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(6),text="6",font=("Courier New",16,'bold')) -but6.place(x=75,y=240) - -but7=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(7),text="7",font=("Courier New",16,'bold')) -but7.place(x=140,y=100) +buttons = [ + ["7", 140, 100], ["8", 140, 170], ["9", 140, 240], + ["4", 75, 100], ["5", 75, 170], ["6", 75, 240], + ["1", 10, 100], ["2", 10, 170], ["3", 10, 240], + ["0", 10, 310], [".", 75, 310], ["+", 205, 100], + ["-", 205, 170], ["*", 205, 240], ["/", 205, 310], + ["CE", 270, 100], ["=", 10, 380], ["√", 270, 240], + ["%", 270, 310], ["^", 270, 170] +] + +for button in buttons: + btn_text, x_pos, y_pos = button + Button(me, padx=14, pady=14, bd=4, bg='white', text=btn_text, + command=lambda btn=btn_text: clickbut(btn) if btn != "=" else calculate_result(), + font=("Courier New", 16, 'bold')).place(x=x_pos, y=y_pos) -but8=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(8),text="8",font=("Courier New",16,'bold')) -but8.place(x=140,y=170) - -but9=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(9),text="9",font=("Courier New",16,'bold')) -but9.place(x=140,y=240) - -but0=Button(me,padx=14,pady=14,bd=4,bg='white',command=lambda:clickbut(0),text="0",font=("Courier New",16,'bold')) -but0.place(x=10,y=310) - -butdot=Button(me,padx=47,pady=14,bd=4,bg='white',command=lambda:clickbut("."),text=".",font=("Courier New",16,'bold')) -butdot.place(x=75,y=310) - -butpl=Button(me,padx=14,pady=14,bd=4,bg='white',text="+",command=lambda:clickbut("+"),font=("Courier New",16,'bold')) -butpl.place(x=205,y=100) - -butsub=Button(me,padx=14,pady=14,bd=4,bg='white',text="-",command=lambda:clickbut("-"),font=("Courier New",16,'bold')) -butsub.place(x=205,y=170) - -butml=Button(me,padx=14,pady=14,bd=4,bg='white',text="*",command=lambda:clickbut("*"),font=("Courier New",16,'bold')) -butml.place(x=205,y=240) - -butdiv=Button(me,padx=14,pady=14,bd=4,bg='white',text="/",command=lambda:clickbut("/"),font=("Courier New",16,'bold')) -butdiv.place(x=205,y=310) - -butclear=Button(me,padx=14,pady=119,bd=4,bg='white',text="CE",command=clrbut,font=("Courier New",16,'bold')) -butclear.place(x=270,y=100) - -butequal=Button(me,padx=151,pady=14,bd=4,bg='white',command=equlbut,text="=",font=("Courier New",16,'bold')) -butequal.place(x=10,y=380) me.mainloop() -