From 09273bda98844385d789cc20e9b29be2ead1bd3c Mon Sep 17 00:00:00 2001 From: almas06 <71059758+almas06@users.noreply.github.com> Date: Sat, 10 Oct 2020 18:52:31 +0530 Subject: [PATCH] Simple Calculator Simple Calculator using Tkinter By Almas Dinani --- Calculator.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Calculator.py diff --git a/Calculator.py b/Calculator.py new file mode 100644 index 0000000..c45d1d5 --- /dev/null +++ b/Calculator.py @@ -0,0 +1,43 @@ +from tkinter import * + +def click(event): + global scrval + text = event.widget.cget("text") + if text == "=": + if scrval.get().isdigit(): + value = int(scrval.get()) + else: + value = eval(screen.get()) + scrval.set(value) + + elif text == "C": + scrval.set("") + screen.update() + else: + scrval.set(scrval.get() + text) + screen.update() + + +root=Tk() +root.geometry("500x550") +root.title("GUI Simple Calculator") + +scrval = StringVar() +scrval.set("") + +screen = Entry(root, textvar=scrval, font="lucida 30 bold", bd=4,relief=SUNKEN) +screen.pack() + +frame = Frame(root, bg="grey", pady=10) +list1 = ["7", "8", "9", "C", "4", "5", "6", "+", "1", "2", "3", "-" ,".", "0", "=", "*", "%", "!", "^", "/"] +i = 0 +for n in list1: + # here width of button means 1 text width + btn = Button(frame, text=n, font="lucida 20 bold", padx=50, width =1,pady=25 ) + btn.grid(row=int(i / 4), column=i % 4) + i = i + 1 + + btn.bind("", click) +frame.pack(fill=X) + +root.mainloop()