Skip to content

Commit 33fdab4

Browse files
authored
Add files via upload
1 parent a08ebdc commit 33fdab4

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

calculator.py

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Python program to create a simple GUI
2+
# calculator using Tkinter
3+
4+
# import everything from tkinter module
5+
from tkinter import *
6+
7+
# globally declare the expression variable
8+
expression = ""
9+
10+
11+
# Function to update expressiom
12+
# in the text entry box
13+
def press(num):
14+
# point out the global expression variable
15+
global expression
16+
17+
# concatenation of string
18+
expression = expression + str(num)
19+
20+
# update the expression by using set method
21+
equation.set(expression)
22+
23+
24+
# Function to evaluate the final expression
25+
def equalpress():
26+
# Try and except statement is used
27+
# for handling the errors like zero
28+
# division error etc.
29+
30+
# Put that code inside the try block
31+
# which may generate the error
32+
try:
33+
34+
global expression
35+
36+
# eval function evaluate the expression
37+
# and str function convert the result
38+
# into string
39+
total = str(eval(expression))
40+
41+
equation.set(total)
42+
43+
# initialze the expression variable
44+
# by empty string
45+
expression = ""
46+
47+
# if error is generate then handle
48+
# by the except block
49+
except:
50+
51+
equation.set(" error ")
52+
expression = ""
53+
54+
55+
# Function to clear the contents
56+
# of text entry box
57+
def clear():
58+
global expression
59+
expression = ""
60+
equation.set("")
61+
62+
63+
# Driver code
64+
if __name__ == "__main__":
65+
# create a GUI window
66+
gui = Tk()
67+
68+
# set the background colour of GUI window
69+
gui.configure(background="light green")
70+
71+
# set the title of GUI window
72+
gui.title("Simple Calculator")
73+
74+
# set the configuration of GUI window
75+
gui.geometry("265x125")
76+
77+
# StringVar() is the variable class
78+
# we create an instance of this class
79+
equation = StringVar()
80+
81+
# create the text entry box for
82+
# showing the expression .
83+
expression_field = Entry(gui, textvariable=equation)
84+
85+
# grid method is used for placing
86+
# the widgets at respective positions
87+
# in table like structure .
88+
expression_field.grid(columnspan=4, ipadx=70)
89+
90+
equation.set('enter your expression')
91+
92+
# create a Buttons and place at a particular
93+
# location inside the root window .
94+
# when user press the button, the command or
95+
# function affiliated to that button is executed .
96+
button1 = Button(gui, text=' 1 ', fg='black', bg='red',
97+
command=lambda: press(1), height=1, width=7)
98+
button1.grid(row=2, column=0)
99+
100+
button2 = Button(gui, text=' 2 ', fg='black', bg='red',
101+
command=lambda: press(2), height=1, width=7)
102+
button2.grid(row=2, column=1)
103+
104+
button3 = Button(gui, text=' 3 ', fg='black', bg='red',
105+
command=lambda: press(3), height=1, width=7)
106+
button3.grid(row=2, column=2)
107+
108+
button4 = Button(gui, text=' 4 ', fg='black', bg='red',
109+
command=lambda: press(4), height=1, width=7)
110+
button4.grid(row=3, column=0)
111+
112+
button5 = Button(gui, text=' 5 ', fg='black', bg='red',
113+
command=lambda: press(5), height=1, width=7)
114+
button5.grid(row=3, column=1)
115+
116+
button6 = Button(gui, text=' 6 ', fg='black', bg='red',
117+
command=lambda: press(6), height=1, width=7)
118+
button6.grid(row=3, column=2)
119+
120+
button7 = Button(gui, text=' 7 ', fg='black', bg='red',
121+
command=lambda: press(7), height=1, width=7)
122+
button7.grid(row=4, column=0)
123+
124+
button8 = Button(gui, text=' 8 ', fg='black', bg='red',
125+
command=lambda: press(8), height=1, width=7)
126+
button8.grid(row=4, column=1)
127+
128+
button9 = Button(gui, text=' 9 ', fg='black', bg='red',
129+
command=lambda: press(9), height=1, width=7)
130+
button9.grid(row=4, column=2)
131+
132+
button0 = Button(gui, text=' 0 ', fg='black', bg='red',
133+
command=lambda: press(0), height=1, width=7)
134+
button0.grid(row=5, column=0)
135+
136+
plus = Button(gui, text=' + ', fg='black', bg='red',
137+
command=lambda: press("+"), height=1, width=7)
138+
plus.grid(row=2, column=3)
139+
140+
minus = Button(gui, text=' - ', fg='black', bg='red',
141+
command=lambda: press("-"), height=1, width=7)
142+
minus.grid(row=3, column=3)
143+
144+
multiply = Button(gui, text=' * ', fg='black', bg='red',
145+
command=lambda: press("*"), height=1, width=7)
146+
multiply.grid(row=4, column=3)
147+
148+
divide = Button(gui, text=' / ', fg='black', bg='red',
149+
command=lambda: press("/"), height=1, width=7)
150+
divide.grid(row=5, column=3)
151+
152+
equal = Button(gui, text=' = ', fg='black', bg='red',
153+
command=equalpress, height=1, width=7)
154+
equal.grid(row=5, column=2)
155+
156+
clear = Button(gui, text='Clear', fg='black', bg='red',
157+
command=clear, height=1, width=7)
158+
clear.grid(row=5, column='1')
159+
160+
# start the GUI
161+
gui.mainloop()

0 commit comments

Comments
 (0)