-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
300 lines (232 loc) · 12.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from tkinter import *
# This is to show message when user gives wrong input
from tkinter.messagebox import *
# from PIL import Image, ImageTk (use this if you want to add images in your gui)
import math
# useful repeating variables
# We are gonna use font multiple times in program so its better to assign it to a variable and use it
font = "lucida 15 bold"
# Important functions
def clickaction(event):
# This will help to get text value which is on buttons
val = event.widget.cget("text") #Storing text value in variable (val)
if val == "x": #In gui we have used alphabet x for multiplication but when we pass that to math module it wont uderstand so we are converting it into *
text.insert(END, "*") #Whenerver you press x on gui it will isert * instead of x
return
# Here the text is variable name which i have defined while creating entry field...
# Here END is index value i.e when i press 7 and then 9... that index value will decide where should that 9 to be shown i.e at the END 79 or somewhere else in this case at start 97
# In the above eg. END means show it after 7 above 2 lines are talking about text.insert(END, val)
elif val == "=":
try:
# This will give you the expression which was written before pressing =
get_expression = text.get()
# print(get_expression) #(Just for uderstanding)
# This function will solve the expression this is inbuilt function in Python/Tkinter
solving_expression = eval(get_expression)
# print(solving_expression) #(Just for uderstanding)
# Upto above code it will show expression and equal to sign (=) on screen but when I press equal button I don't want to see expression and
# equal button like this eg.3+4= i want direct result i.e 7 to do that
text.delete(0, END) # It will delete everything which is on screen
text.insert(0, solving_expression) #This will insert/show solved expression on screen
# after inserting answer i will return the function else it will insert lines which are after this if else statement
except Exception as e:
showerror("Error", "Invalid input")
return
# To clear all data on screen i.e making clear button fuctioning
elif val == "C":
text.delete(0, END)
return
# Agar mai return na likhu toh val ki value yaha pe hogi C jo niche jage insert hogi aur screen pe dikhegi to avoid that function ko
# yahi se return karo
# To clear one digit at a time i.e making backspace button fuctioning
elif val == "⌫":
get_data = text.get() # get all data which is in text field
# Take all data except last digit and assign it to modify_data
modify_data = get_data[0:len(get_data)-1]
text.delete(0, END) # delete everything on screen
# insert the data which is in modify data at index 0
text.insert(0, modify_data)
return
text.insert(END, val)
# creating a window
root = Tk()
root.title("Calculator")
root.geometry("400x600")
root.config(bg="pink") #Changing root background
# To change it's icon ....the file should be in (.ico) format
root.iconbitmap("image/calicon.ico")
# adding textfield (Here we see all the values when we press button)
# To change height of entry widget we have to increase font size there is no other option (till now)
text = Entry(root, font="lucida 25 bold", justify=CENTER)
text.pack(side=TOP, pady=10, padx=5, fill=X)
# frame is like div container
# creating one frame to put buttons inside that frame
buttonframe = Frame(root)
buttonframe.pack(side=TOP) #side will decide where to put that frame. TOP (default), BOTTOM, LEFT, or RIGHT.
# Adding buttons in frame(buttonframe)
# By using grid geometry we can devide our root window or frame into rows and columns like excel so that we could put calculator buttons properly.
# In grid we use concept of excel (0,0)th cell (0,1)th cell.....
# Creating 1 to 9 buttons
initial_value = 9
for i in range(0, 3):
for j in range(2, -1, -1):
btn = Button(buttonframe, text=initial_value, font=font, width=5, height=3, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="pink")
btn.grid(row=i, column=j, padx=2, pady=2)
btn.bind('<Button-1>', clickaction)
initial_value -= 1
# Binding 1 to 9 buttons so i could fetch the data in that button
# Here <Button-1> means when i take my cursor on some button and right click on it it should call some function
# Creating other buttons
Zerobtn = Button(buttonframe, text="0", font=font, width=5, height=3, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
Zerobtn.grid(row=3, column=1, padx=2, pady=2)
# Creating equal button using one liner code
# when u create one liner code it will create problem while binding so its better to assign it to some variable below code will also work
# Button(buttonframe, text="=", font=font, width=5, height=3, pady=5, relief="ridge",
# activebackground="grey", activeforeground="white").grid(row=3, column=2, padx=2, pady=2)
equalbtn = Button(buttonframe, text="=", font=font, width=5, height=3, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="pink")
equalbtn.grid(row=3, column=2, padx=2, pady=2)
# Creating dot button
dotbtn = Button(buttonframe, text=".", font=font, width=5, height=3, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
dotbtn.grid(row=3, column=0, padx=2, pady=2)
# Creating operation buttons +,-,=,/
divisionbtn = Button(buttonframe, text="/", font=font, width=5, height=3, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
divisionbtn.grid(row=0, column=3, padx=2, pady=2)
multiplicationbtn = Button(buttonframe, text="x", font=font, width=5, height=3, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
multiplicationbtn.grid(row=1, column=3, padx=2, pady=2)
substractionbtn = Button(buttonframe, text="-", font=font, width=5, height=3, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
substractionbtn.grid(row=2, column=3, padx=2, pady=2)
additionbtn = Button(buttonframe, text="+", font=font, width=5, height=3, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
additionbtn.grid(row=3, column=3, padx=2, pady=2)
# Creating clear and back button
# To combine 2 columns we can use columnspan=2 in grid
clearbtn = Button(buttonframe, text="⌫", font=font, width=11, height=2, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
clearbtn.grid(row=4, column=0, padx=1, pady=2, columnspan=2)
allclearbtn = Button(buttonframe, text="C", font=font, width=11, height=2, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="pink")
allclearbtn.grid(row=4, column=2, padx=1, pady=2, columnspan=2)
# Binding all remaining buttons
equalbtn.bind('<Button-1>', clickaction)
Zerobtn.bind('<Button-1>', clickaction)
dotbtn.bind('<Button-1>', clickaction)
divisionbtn.bind('<Button-1>', clickaction)
multiplicationbtn.bind('<Button-1>', clickaction)
substractionbtn.bind('<Button-1>', clickaction)
additionbtn.bind('<Button-1>', clickaction)
clearbtn.bind('<Button-1>', clickaction)
allclearbtn.bind('<Button-1>', clickaction)
# ....................Coding for scintific calculator....................
# Creating frame for scintific calculator
sciFrame = Frame(root)
sqrt = Button(sciFrame, text="√", font=font, width=5, height=2, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="orange")
sqrt.grid(row=0, column=0, padx=2, pady=2)
power = Button(sciFrame, text="^", font=font, width=5, height=2, pady=5,
relief="ridge", activebackground="grey", activeforeground="white",bg="orange")
power.grid(row=0, column=1, padx=2, pady=2)
factorialbtn = Button(sciFrame, text="x!", font=font, width=5, height=2, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="orange")
factorialbtn.grid(row=0, column=2, padx=2, pady=2)
radbtn = Button(sciFrame, text="Rad", font=font, width=5, height=2, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="orange")
radbtn.grid(row=0, column=3, padx=2, pady=2)
sinbtn = Button(sciFrame, text="sin", font=font, width=5, height=2, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="orange")
sinbtn.grid(row=1, column=0, padx=2, pady=2)
cosbtn = Button(sciFrame, text="cos", font=font, width=5, height=2, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="orange")
cosbtn.grid(row=1, column=1, padx=2, pady=2)
tanbtn = Button(sciFrame, text="tan", font=font, width=5, height=2, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="orange")
tanbtn.grid(row=1, column=2, padx=2, pady=2)
degbtn = Button(sciFrame, text="Deg", font=font, width=5, height=2, pady=5, relief="ridge",
activebackground="grey", activeforeground="white",bg="orange")
degbtn.grid(row=1, column=3, padx=2, pady=2)
# Creating functions ...
# Write functions before binding else it will throw an error NameError: name 'sci_cal' is not defined
def sci_cal(event):
val = event.widget.cget("text") #To get text value
# print(val) #(Just for Understanding purpose)
expression=text.get() #To get value
# print(expression) #(Just for Understanding purpose)
if val=="Deg":
ans=math.degrees(float(expression)) #Storing solution/answer in ans variable
elif val=="Rad":
ans=math.radians(float(expression[0]),2)
elif val=="sin":
ans=math.sin(math.radians(float(expression))) #Sin takes value in rad so we are converting degree into radian
elif val=="cos":
ans=math.cos(math.radians(float(expression)))
elif val=="tan":
ans=math.tan(math.radians(float(expression)))
elif val=="√":
ans=math.sqrt(float(expression))
elif val=="^":
text.insert(END, "**")
return
elif val=="=":
ans=math.pow(float(expression),2)
elif val=="x!":
ans=math.factorial(float(expression))
text.delete(0, END) # It will delete everythin which is on screen
text.insert(0,ans) #It will insert value of ans at index 0
# Binding all buttons
sqrt.bind('<Button-1>', sci_cal)
power.bind('<Button-1>', sci_cal)
factorialbtn.bind('<Button-1>', sci_cal)
radbtn.bind('<Button-1>', sci_cal)
sinbtn.bind('<Button-1>', sci_cal)
cosbtn.bind('<Button-1>', sci_cal)
tanbtn.bind('<Button-1>', sci_cal)
degbtn.bind('<Button-1>', sci_cal)
#Making keyboard's equal button fuctioning
def keybrdequalbtn(event):
getvalue = Event() #Creating event object
getvalue.widget=equalbtn #Assigning equal button
clickaction(getvalue)
text.bind('<Return>',keybrdequalbtn)
#Writing code to shift between normal mode and scientific mode
x = "Normal Mode"
def ScientificCal_click():
global x
x = modename.entrycget(0, "label")
if x == "Normal Mode":
modename.add_command(label="Scientific Cal",
command=ScientificCal_click) # Adding options
# concept : self.filemenu2.delete(0) # deletes first item in menu
modename.delete(0)
# self.filemenu2.delete("Stop") $ delete item with the label "Stop"
sciFrame.pack_forget()
root.geometry("400x600")
else:
modename.add_command(label="Normal Mode",
command=ScientificCal_click) # Adding options
modename.delete(0)
buttonframe.pack_forget()
sciFrame.pack(side=TOP)
buttonframe.pack(side=TOP)
root.geometry("400x750")
# Adding menubar
menubar = Menu(root, font="lucida 10 bold") # This line says root ke andar ek menu banao
# Adding options/modes in Menu
# Aur ek menu banao jo menubar be hoga ....tearoff removes dotted line
modename = Menu(menubar, font="lucida 10 bold", tearoff=0)
modename.add_command(label="Scientific Cal",
command=ScientificCal_click) # Adding options
# If u dont cascade it wont show it on screen
menubar.add_cascade(label="Mode", menu=modename)
root.config(menu=menubar)
root.mainloop()
#If u want to convert .py file to .exe file...............
#First install pyinstaller module
#While converting this .py file to .exe use command pyinstaller --onefile main.py (whatever ur filename is )
#If you use above command it will convert it into .exe file but when you open that exe file it will open your file along with black console in background
#To get rid of that console use (-w)...i.e... pyinstaller --onefile -w main.py it will solve the problem now it will open only your gui application