Skip to content

Commit 47c06b2

Browse files
authored
Create basic-calculator-gui.py
1 parent 2f953b1 commit 47c06b2

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# the calculator
2+
from tkinter import Tk, Button, Label, Grid, Entry, END
3+
4+
root = Tk()
5+
root.title("CALCULATOR")
6+
e = Entry(
7+
root,
8+
width=35,
9+
borderwidth=10,
10+
)
11+
e.grid(row=0, column=0, columnspan=4, padx=8, pady=8)
12+
13+
14+
def joker(number):
15+
now = e.get()
16+
e.delete(0, END)
17+
e.insert(0, str(now) + str(number))
18+
19+
20+
def addd():
21+
n1 = e.get()
22+
global num1
23+
global cal
24+
cal = "add"
25+
num1 = float(n1)
26+
e.delete(0, END)
27+
28+
29+
def sub():
30+
n1 = e.get()
31+
global num1
32+
global cal
33+
cal = "sub"
34+
num1 = float(n1)
35+
e.delete(0, END)
36+
37+
38+
def mul():
39+
n1 = e.get()
40+
global num1
41+
global cal
42+
cal = "mul"
43+
num1 = float(n1)
44+
e.delete(0, END)
45+
46+
47+
def div():
48+
n1 = e.get()
49+
global num1
50+
global cal
51+
cal = "div"
52+
num1 = float(n1)
53+
e.delete(0, END)
54+
55+
56+
def equ():
57+
if cal == "add":
58+
n2 = e.get()
59+
e.delete(0, END)
60+
e.insert(0, num1 + float(n2))
61+
elif cal == "sub":
62+
n2 = e.get()
63+
e.delete(0, END)
64+
e.insert(0, num1 - float(n2))
65+
elif cal == "mul":
66+
n2 = e.get()
67+
e.delete(0, END)
68+
e.insert(0, num1 * float(n2))
69+
elif cal == "div":
70+
n2 = e.get()
71+
e.delete(0, END)
72+
e.insert(0, num1 / float(n2))
73+
else:
74+
pass
75+
76+
77+
def clr():
78+
e.delete(0, END)
79+
80+
81+
# bottons
82+
btn1 = Button(root, text="1", padx=20, pady=5, command=lambda: joker("1"))
83+
btn2 = Button(root, text="2", padx=20, pady=5, command=lambda: joker("2"))
84+
btn3 = Button(root, text="3", padx=20, pady=5, command=lambda: joker("3"))
85+
btn4 = Button(root, text="4", padx=20, pady=5, command=lambda: joker("4"))
86+
btn5 = Button(root, text="5", padx=20, pady=5, command=lambda: joker("5"))
87+
btn6 = Button(root, text="6", padx=20, pady=5, command=lambda: joker("6"))
88+
btn7 = Button(root, text="7", padx=20, pady=5, command=lambda: joker("7"))
89+
btn8 = Button(root, text="8", padx=20, pady=5, command=lambda: joker("8"))
90+
btn9 = Button(root, text="9", padx=20, pady=5, command=lambda: joker("9"))
91+
btn0 = Button(root, text="0", padx=20, pady=5, command=lambda: joker("0"))
92+
btnadd = Button(root, text="+", padx=20, pady=5, command=addd)
93+
btnsub = Button(root, text="-", padx=20, pady=5, command=sub)
94+
btnmul = Button(root, text="*", padx=20, pady=5, command=mul)
95+
btndiv = Button(root, text="/", padx=20, pady=5, command=div)
96+
btnequ = Button(root, text="=", padx=20, pady=5, command=equ)
97+
btndot = Button(root, text=".", padx=20, pady=5, command=lambda: joker("."))
98+
btnclr = Button(root, text="clear", padx=20, pady=5, command=clr)
99+
100+
101+
btn1.grid(row=3, column=0)
102+
btn2.grid(row=3, column=1)
103+
btn3.grid(row=3, column=2)
104+
btn4.grid(row=2, column=0)
105+
btn5.grid(row=2, column=1)
106+
btn6.grid(row=2, column=2)
107+
btn7.grid(row=1, column=0)
108+
btn8.grid(row=1, column=1)
109+
btn9.grid(row=1, column=2)
110+
btn0.grid(row=4, column=1)
111+
btnadd.grid(row=1, column=3)
112+
btnsub.grid(row=2, column=3)
113+
btnmul.grid(row=3, column=3)
114+
btndiv.grid(row=4, column=3)
115+
btnequ.grid(row=4, column=0)
116+
btndot.grid(row=4, column=2)
117+
btnclr.grid(row=5, column=1)
118+
119+
root.mainloop()

0 commit comments

Comments
 (0)