Skip to content

Commit 8d56f10

Browse files
Merge pull request souravjain540#27 from Manice18/main
2 parents 175c8fb + 6689f8f commit 8d56f10

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

guicalculator.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from tkinter import *
2+
3+
root = Tk()
4+
root.title("Simple Calculator")
5+
6+
data = Entry(root, width=35, borderwidth=5)
7+
data.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
8+
9+
#data.insert(0, "")
10+
11+
def button_click(number):
12+
#data.delete(0, END)
13+
current = data.get()
14+
data.delete(0, END)
15+
data.insert(0, str(current) + str(number))
16+
17+
def button_clear():
18+
data.delete(0, END)
19+
20+
def button_add():
21+
first_number = data.get()
22+
global f_num
23+
global math
24+
math = "addition"
25+
f_num = int(first_number)
26+
data.delete(0, END)
27+
28+
def button_equal():
29+
second_number = data.get()
30+
data.delete(0, END)
31+
32+
if math == "addition":
33+
data.insert(0, f_num + int(second_number))
34+
35+
if math == "subtraction":
36+
data.insert(0, f_num - int(second_number))
37+
38+
if math == "multiplication":
39+
data.insert(0, f_num * int(second_number))
40+
41+
if math == "division":
42+
data.insert(0, f_num / int(second_number))
43+
44+
45+
46+
def button_subtract():
47+
first_number = data.get()
48+
global f_num
49+
global math
50+
math = "subtraction"
51+
f_num = int(first_number)
52+
data.delete(0, END)
53+
54+
def button_multiply():
55+
first_number = data.get()
56+
global f_num
57+
global math
58+
math = "multiplication"
59+
f_num = int(first_number)
60+
data.delete(0, END)
61+
62+
def button_divide():
63+
first_number = data.get()
64+
global f_num
65+
global math
66+
math = "division"
67+
f_num = int(first_number)
68+
data.delete(0, END)
69+
70+
71+
# Define Buttons
72+
73+
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
74+
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
75+
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
76+
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
77+
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
78+
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
79+
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
80+
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
81+
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
82+
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
83+
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
84+
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
85+
button_clear = Button(root, text="Clear", padx=79, pady=20, command=button_clear)
86+
87+
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
88+
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
89+
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)
90+
91+
# Put the buttons on the screen
92+
93+
button_1.grid(row=3, column=0)
94+
button_2.grid(row=3, column=1)
95+
button_3.grid(row=3, column=2)
96+
97+
button_4.grid(row=2, column=0)
98+
button_5.grid(row=2, column=1)
99+
button_6.grid(row=2, column=2)
100+
101+
button_7.grid(row=1, column=0)
102+
button_8.grid(row=1, column=1)
103+
button_9.grid(row=1, column=2)
104+
105+
button_0.grid(row=4, column=0)
106+
button_clear.grid(row=4, column=1, columnspan=2)
107+
button_add.grid(row=5, column=0)
108+
button_equal.grid(row=5, column=1, columnspan=2)
109+
110+
button_subtract.grid(row=6, column=0)
111+
button_multiply.grid(row=6, column=1)
112+
button_divide.grid(row=6, column=2)
113+
114+
115+
root.mainloop()

0 commit comments

Comments
 (0)