Skip to content

Major Updated and fixes #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# GUI-scientific-calculator-using-python
it's a scientific calculator using python.It will consist a graphical user interface using Tkinter libraries in python.
Hey Guys it's a python GUI (Made with Tkinter) Calculator project.

To Create the Application:-
1) Importing the module – tkinter
2) Create the main window (container)
3) Add any number of widgets to the main window
4) Apply the event Trigger on the widgets.

Let’s create a GUI based simple calculator using Python Tkinter module, which can perform basic arithmatic operations addition, subtraction, multiplication and division.

Have a Look at my code (main.py)......

Follow Me on :---

#GitHub - https://github.com/deepraj02

#Instagram - https://www.instagram.com/deeprajbaidya02/?hl=en

Thanks,
HAPPY CODING........


With Regards,

Deepraj Baidya.
252 changes: 136 additions & 116 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,117 +1,137 @@
from tkinter import *


class calculate():

def __init__(self):
self.root = Tk()
self.root.title("Calculator")
self.root.geometry("370x220")

self.resultwindow = Entry(self.root)
self.resultwindow.grid(row=0,column=0,columnspan=6)
self.resultwindow.config(font=("Arial", 18))
self.resultwindow.focus_set() # Sets focus on the input text area

# Buttons
self.button1 = Button(self.root, text="1", width=3, command=lambda:self.ins('1'))
self.button1.grid(row=1,column=0, padx=3, pady=3)
self.button1.config(font=("Arial", 18))

self.button2 = Button(self.root, text="2", width=3, command=lambda:self.ins('2'))
self.button2.grid(row=1, column=1, padx=3, pady=3)
self.button2.config(font=("Arial", 18))

self.button3 = Button(self.root, text="3", width=3, command=lambda:self.ins('3'))
self.button3.grid(row=1, column=2, padx=3, pady=3)
self.button3.config(font=("Arial", 18))

self.button4 = Button(self.root, text="4", width=3, command=lambda:self.ins('4'))
self.button4.grid(row=2, column=0, padx=3, pady=3)
self.button4.config(font=("Arial", 18))

self.button5 = Button(self.root, text="5", width=3, command=lambda:self.ins('5'))
self.button5.grid(row=2, column=1, padx=3, pady=3)
self.button5.config(font=("Arial", 18))

self.button6 = Button(self.root, text="6", width=3, command=lambda:self.ins('6'))
self.button6.grid(row=2, column=2, padx=3, pady=3)
self.button6.config(font=("Arial", 18))

self.button7 = Button(self.root, text="7", width=3, command=lambda:self.ins('7'))
self.button7.grid(row=3, column=0, padx=3, pady=3)
self.button7.config(font=("Arial", 18))

self.button8 = Button(self.root, text="8", width=3, command=lambda:self.ins('8'))
self.button8.grid(row=3, column=1, padx=3, pady=3)
self.button8.config(font=("Arial", 18))

self.button9 = Button(self.root, text="9", width=3, command=lambda:self.ins('9'))
self.button9.grid(row=3, column=2, padx=3, pady=3)
self.button9.config(font=("Arial", 18))

self.button0 = Button(self.root, text="0", width=3, command=lambda: self.ins('0'))
self.button0.grid(row=4, column=0, padx=3, pady=3)
self.button0.config(font=("Arial", 18))

self.button_open = Button(self.root, text="(", width=3, command=lambda: self.ins('('))
self.button_open.grid(row=4, column=1, padx=3, pady=3)
self.button_open.config(font=("Arial", 18))

self.button_close = Button(self.root, text=")", width=3, command=lambda: self.ins(')'))
self.button_close.grid(row=4, column=2, padx=3, pady=3)
self.button_close.config(font=("Arial", 18))

# Operations Buttons

self.buttonplus = Button(self.root, text="+", width=3, command=lambda:self.ins('+'))
self.buttonplus.grid(row=1, column=3, padx=3, pady=3)
self.buttonplus.config(font=("Arial", 18))

self.buttonminus = Button(self.root, text="-", width=3, command=lambda:self.ins('-'))
self.buttonminus.grid(row=1, column=4, padx=3, pady=3)
self.buttonminus.config(font=("Arial", 18))

self.buttondivide = Button(self.root, text="/", width=3, command=lambda:self.ins('/'))
self.buttondivide.grid(row=2, column=3, padx=3, pady=3)
self.buttondivide.config(font=("Arial", 18))

self.buttonmultiply = Button(self.root, text="*", width=3, command=lambda:self.ins('*'))
self.buttonmultiply.grid(row=2, column=4, padx=3, pady=3)
self.buttonmultiply.config(font=("Arial", 18))

self.buttoncancel = Button(self.root, text="C", width=3, command=lambda: self.cancel())
self.buttoncancel.grid(row=3, column=4, padx=3, pady=3)
self.buttoncancel.config(font=("Arial", 18))

self.buttondeleteall = Button(self.root, text="Del", width=3, command=lambda: self.delete_all())
self.buttondeleteall.grid(row=3, column=3, padx=3, pady=3)
self.buttondeleteall.config(font=("Arial", 18))

self.buttonresult = Button(self.root, text="=", width=6, command=lambda:self.calculate())
self.buttonresult.grid(row=4, column=3, padx=3, pady=3, columnspan=2)
self.buttonresult.config(font=("Arial", 18))

self.root.mainloop()

def ins(self,val):
self.resultwindow.insert(END, val)

def cancel(self):
self.resultwindow.delete(0, 'end')

def delete_all(self):
x = self.resultwindow.get()
self.resultwindow.delete(0, 'end')
y = x[:-1]
self.resultwindow.insert(0, y)

def calculate(self):
x = self.resultwindow.get()
answer = eval(x)
self.resultwindow.delete(0, 'end')
self.resultwindow.insert(0, answer)


if __name__ == "__main__":
calculate()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall approach was Nice but The GUI sucks

import tkinter.messagebox as tmb

#& All functions
def click(event):
global scvalue
text = event.widget.cget("text")
if text == "=":
if scvalue.get().isdigit():
value = int(scvalue.get())
else:
try:
value = eval(screen.get())

except Exception as e:
print(e)
err=tmb.showerror("ERROR ENCOUNTERED","SORRY, An Error Occured,\n Please Check Your Input.")
scvalue.set(value)
screen.update()

elif text == "C":
scvalue.set("")
screen.update()

else:
scvalue.set(scvalue.get() + text)
screen.update() #@ to update the screen with the widgets pressed


root=Tk()
root.geometry("390x498")
root.minsize(390,498)
root.maxsize(390,498)
root.title("GUI Calculator -By Deepraj")
root.wm_iconbitmap("calculator_14445.ico")




#$ Moving to Screen
scvalue=StringVar()
scvalue.set("")
screen=Entry(root,textvar=scvalue,font="Comicsansms 30",bg="#B5BABA",relief=SUNKEN)
screen.pack(pady=10,padx=4)

#^ BAsic
f1=Frame(root,bg="#EE5537",height=20,width=30)
b=Button(f1,text="C",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="/",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=E)
b.bind("<Button-1>",click)
b=Button(f1,text="-",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=E)
b.bind("<Button-1>",click)

f1.pack(anchor=NW)

f1=Frame(root,bg="#EE5537",height=20,width=30)

b=Button(f1,text="+",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=E)
b.bind("<Button-1>",click)
b=Button(f1,text="*",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="=",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
f1.pack(anchor=NW)


#! First Row Buttons
f1=Frame(root,bg="#EE5537",height=20,width=30)
b=Button(f1,text="7",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="8",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="9",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
f1.pack(anchor=NW)

#^ Second row Buttons

f1=Frame(root,bg="#EE5537",height=20,width=30)
b=Button(f1,text="4",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="5",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="6",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
f1.pack(anchor=NW)

#* Third row Buttons
f1=Frame(root,bg="#EE5537",padx=0,height=20,width=30)
b=Button(f1,text="1",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="2",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="3",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
f1.pack(anchor=NW)

#@ Fourth row Buttons
f1=Frame(root,bg="#EE5537",height=20,width=20)
b=Button(f1,text="00",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text="0",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
b=Button(f1,text=".",font="consolas 19",bg="#E8F2E6",width=8)
b.pack(side=LEFT,padx=3,pady=2,anchor=W)
b.bind("<Button-1>",click)
f1.pack(anchor=NW)

#% Thanks Func
def thanks():
tmb.showinfo("Feedback-Window","Would you like to give us a 5⭐\n Enjoy Your day....")

#$ Thanks Note
f1=Frame(root,bg="#EE5537",height=10,width=15)
b=Button(f1,text="Share your\nExperience⭐",font="consolas 19",bg="#E8F2E6",width=12,command=thanks)
b.pack(side=BOTTOM,padx=3,pady=4,anchor=W)
f1.pack(anchor=S)

root.mainloop()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more accurate and charming GUI with fast execution of the Program

Binary file added calculator_14445.ico
Binary file not shown.
Loading