-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathscript.py
190 lines (170 loc) · 5.48 KB
/
script.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
import tkinter as tk
root = tk.Tk() # Main box window
root.title("Standard Calculator") # Title shown at the title bar
root.resizable(0, 0) # disabling the resizeing of the window
# Creating an entry field:
e = tk.Entry(root,
width=35,
bg='#f0ffff',
fg='black',
borderwidth=5,
justify='right',
font='Calibri 15')
e.grid(row=0, column=0, columnspan=3, padx=12, pady=12)
def buttonClick(num): # function for clicking
temp = e.get(
) # temporary varibale to store the current input in the screen
e.delete(0, tk.END) # clearing the screen from index 0 to END
e.insert(0, temp + num) # inserting the incoming number input
def buttonClear(): # function for clearing
e.delete(0, tk.END)
def buttonGet(
oper
): # function for storing the first input and printing '+, -, /, *'
global num1, math # global variable num1 and math to use in function buttonEqual()
num1 = e.get() # getting first number
math = oper # oper varaible is the type of operation being performed
e.insert(tk.END, math)
try:
num1 = float(num1) # converting the number to float type
except ValueError: # in case there is a character other than numerals, clear the screen
buttonClear()
def buttonEqual(): # function for printing the sum
inp = e.get() # getting the inserted input
num2 = float(inp[inp.index(math) + 1:]) # getting the second number
e.delete(0, tk.END)
if math == '+': # Addition
e.insert(0, str(num1 + num2))
elif math == '-': # Subtraction
e.insert(0, str(num1 - num2))
elif math == 'x': # Multiplication
e.insert(0, str(num1 * num2))
elif math == '/': # Division
try:
e.insert(0, str(num1 / num2))
except ZeroDivisionError:
# in case there is a zero in the denominator, answer is undefined
e.insert(0, 'Undefined')
# Defining Buttons:
b1 = tk.Button(root,
text='1',
padx=40,
pady=10,
command=lambda: buttonClick('1'),
font='Calibri 12')
b2 = tk.Button(root,
text='2',
padx=40,
pady=10,
command=lambda: buttonClick('2'),
font='Calibri 12')
b3 = tk.Button(root,
text='3',
padx=40,
pady=10,
command=lambda: buttonClick('3'),
font='Calibri 12')
b4 = tk.Button(root,
text='4',
padx=40,
pady=10,
command=lambda: buttonClick('4'),
font='Calibri 12')
b5 = tk.Button(root,
text='5',
padx=40,
pady=10,
command=lambda: buttonClick('5'),
font='Calibri 12')
b6 = tk.Button(root,
text='6',
padx=40,
pady=10,
command=lambda: buttonClick('6'),
font='Calibri 12')
b7 = tk.Button(root,
text='7',
padx=40,
pady=10,
command=lambda: buttonClick('7'),
font='Calibri 12')
b8 = tk.Button(root,
text='8',
padx=40,
pady=10,
command=lambda: buttonClick('8'),
font='Calibri 12')
b9 = tk.Button(root,
text='9',
padx=40,
pady=10,
command=lambda: buttonClick('9'),
font='Calibri 12')
b0 = tk.Button(root,
text='0',
padx=40,
pady=10,
command=lambda: buttonClick('0'),
font='Calibri 12')
bdot = tk.Button(root,
text='.',
padx=41,
pady=10,
command=lambda: buttonClick('.'),
font='Calibri 12')
badd = tk.Button(root,
text='+',
padx=29,
pady=10,
command=lambda: buttonGet('+'),
font='Calibri 12')
bsub = tk.Button(root,
text='-',
padx=30,
pady=10,
command=lambda: buttonGet('-'),
font='Calibri 12')
bmul = tk.Button(root,
text='x',
padx=30,
pady=10,
command=lambda: buttonGet('x'),
font='Calibri 12')
bdiv = tk.Button(root,
text='/',
padx=30.5,
pady=10,
command=lambda: buttonGet('/'),
font='Calibri 12')
bclear = tk.Button(root,
text='AC',
padx=20,
pady=10,
command=buttonClear,
font='Calibri 12')
bequal = tk.Button(root,
text='=',
padx=39,
pady=10,
command=buttonEqual,
font='Calibri 12')
# Putting the buttons on the screen:
b1.grid(row=3, column=0)
b2.grid(row=3, column=1)
b3.grid(row=3, column=2)
badd.grid(row=3, column=3)
b4.grid(row=2, column=0)
b5.grid(row=2, column=1)
b6.grid(row=2, column=2)
bmul.grid(row=2, column=3)
b7.grid(row=1, column=0)
b8.grid(row=1, column=1)
b9.grid(row=1, column=2)
bdiv.grid(row=1, column=3)
b0.grid(row=4, column=0)
bdot.grid(row=4, column=1)
bequal.grid(row=4, column=2)
bsub.grid(row=4, column=3)
bclear.grid(row=0, column=3)
# Looping the window:
root.mainloop()