-
Notifications
You must be signed in to change notification settings - Fork 28
/
Simple Calculator by Python.py
206 lines (158 loc) · 6.34 KB
/
Simple Calculator by Python.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
"""
@Author: Pranta Sarker
Institute: North East University Bangladesh
Language: Python
Version: 3.x
Platfrom: Pycharm Community Version
"""
from tkinter import *;
from tkinter import messagebox;
def actionauthor():
messagebox.showinfo("Author", "Pranta Sarker\nBatch: 6th\nDepartment: CSE\nNorth East University Bangladesh")
#Check weather the input string is a number or not
def is_number(s):
if(s != ''):
if (s.replace('.', '', 1).isdigit()):
return True
if (s.isdigit()):
return True;
if s[0] in ['-', '+', '.', '0', ' ']:
if (s[1] == '.'):
if (s[2:].isdigit()):
return True
if (s[1] == '0' and s[2] == '.'):
if (s[3:].isdigit()):
return True
if s[1:].isdigit():
return True;
return False;
def casting(num):
if('.' in num):
return float(num);
else:
return int(num)
#Plus sign function
def actionPlus():
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='red', bg='#9ed8ee')
Showtemplabel.insert(0, 'Summation');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
ans = "0";
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
num1 = Numberentry1.get();
num2 = Numberentry2.get();
if(is_number(num1) == True and is_number(num2) == True and num1 != ' ' and num2 != ' '):
num1 = casting(num1);
num2 = casting(num2);
ans = str(num1 + num2);
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='red', bg='#9ed8ee')
Showtemplabel.insert(0, 'Summation');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
else:
messagebox.showerror("Error", "Enter a Valid number\ne.g. 123, 0.123, .123, -0.123, 123.456")
#Minus sign function
def actionMinus():
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='green', bg='#ece7e2')
Showtemplabel.insert(0, 'Subtraction');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
ans = "0";
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
num1 = Numberentry1.get();
num2 = Numberentry2.get();
if(is_number(num1)==True and is_number(num2)==True):
num1 = casting(num1);
num2 = casting(num2);
ans = str(num1 - num2);
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='green', bg='#ece7e2')
Showtemplabel.insert(0, 'Subtraction');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
else:
messagebox.showerror("Error", "Enter a Valid number\ne.g. 123, 0.123, .123, -0.123, 123.456")
#Multiplication sign function
def actionMul():
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='blue', bg='#cacba9')
Showtemplabel.insert(0, 'Multiplication');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
ans = "0"
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
num1 = Numberentry1.get();
num2 = Numberentry2.get();
if(is_number(num1)==True and is_number(num2)==True):
num1 = casting(num1);
num2 = casting(num2);
ans = str(num1 * num2);
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='blue', bg='#cacba9')
Showtemplabel.insert(0, 'Multiplication');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
else:
messagebox.showerror("Error", "Enter a Valid number\ne.g. 123, 0.123, .123, -0.123, 123.456")
#Division sign function
def actionDiv():
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='yellow', bg='#8dad96')
Showtemplabel.insert(0, 'Division');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
ans = "0"
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
num1 = Numberentry1.get();
num2 = Numberentry2.get();
if(is_number(num1)==True and is_number(num2)==True):
num1 = casting(num1);
num2 = casting(num2);
ans = str(num1 / num2);
Showtemplabel.delete(0, END);
Showlabel.delete(0, END)
Showtemplabel.config(fg='yellow', bg='#8dad96')
Showtemplabel.insert(0, 'Division');
Showtemplabel.place(relx=0.5, rely=0.5, anchor='center')
Showlabel.insert(0, ans);
Showlabel.place(relx=0.5, rely=0.6, anchor='center')
else:
messagebox.showerror("Error", "Enter a Valid number\ne.g. 123, 0.123, .123, -0.123, 123.456")
root = Tk();
root.title('My First Python Calculator');
root.geometry('380x300+200+250');
Titlelabel = Label(root, fg = 'green' , font = 'none 10 bold underline' ,text = 'Python Calculator', compound = CENTER)
Titlelabel.place(relx=0.5, rely=0.1, anchor='center')
Showlabel = Entry(root);
Showtemplabel = Entry(root);
Numberentry1 = Entry(root);
Numberentry2 = Entry(root);
Numberentry1.place(relx=0.5, rely=0.3, anchor='center')
Numberentry2.place(relx=0.5, rely=0.4, anchor='center')
plusbutton = Button(root, text="+", width = 5, command = actionPlus);
plusbutton.place(relx=0.1, rely=0.7)
minusbutton = Button(root, text="-", width = 5, command = actionMinus);
minusbutton.place(relx=0.3, rely=0.7)
mulbutton = Button(root, text="*", width = 5, command = actionMul);
mulbutton.place(relx=0.5, rely=0.7)
divbutton = Button(root, text="/", width = 5, command = actionDiv);
divbutton.place(relx=0.7, rely=0.7)
authorbutton = Button(root, text='Author', width=6, command = actionauthor);
authorbutton.place(relx = 0.5, rely=0.95, anchor='center');
root.resizable(False, False);
root.mainloop();