-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
179 lines (133 loc) · 5.55 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
# Calculator
import tkinter as tk
from tkinter import ttk
def input_calculator_1():
pos = len(entry.get())
entry.insert(pos, "1")
def input_calculator_2():
pos = len(entry.get())
entry.insert(pos, "2")
def input_calculator_3():
pos = len(entry.get())
entry.insert(pos, "3")
def input_calculator_4():
pos = len(entry.get())
entry.insert(pos, "4")
def input_calculator_5():
pos = len(entry.get())
entry.insert(pos, "5")
def input_calculator_6():
pos = len(entry.get())
entry.insert(pos, "6")
def input_calculator_7():
pos = len(entry.get())
entry.insert(pos, "7")
def input_calculator_8():
pos = len(entry.get())
entry.insert(pos, "8")
def input_calculator_9():
pos = len(entry.get())
entry.insert(pos, "9")
def input_calculator_0():
pos = len(entry.get())
entry.insert(pos, "0")
def input_calculator_p():
pos = len(entry.get())
entry.insert(pos, ".")
def input_calculator_plus():
pos = len(entry.get())
entry.insert(pos, "+")
def input_calculator_minus():
pos = len(entry.get())
entry.insert(pos, "-")
def input_calculator_mult():
pos = len(entry.get())
entry.insert(pos, "*")
def input_calculator_div():
pos = len(entry.get())
entry.insert(pos, "/")
def input_calculator_percent():
pos = len(entry.get())
entry.insert(pos, "%")
def input_calculator_par_open():
pos = len(entry.get())
entry.insert(pos, "(")
def input_calculator_par_close():
pos = len(entry.get())
entry.insert(pos, ")")
def clear():
end = len(entry.get())
entry.delete(0, end)
def calculate():
calc = entry.get()
calc_list = list(calc)
calc_list_len = len(calc_list)
for i in range(calc_list_len):
if calc_list[i] == "%":
calc_list[i] = "*1/100"
# print(calc)
calc = ''.join(calc_list)
result = eval(calc)
end = len(entry.get())
entry.delete(0, end)
entry.insert(0, result)
if __name__ == "__main__":
root = tk.Tk()
root.title("Calculator")
root.geometry("400x300")
root.resizable(False, False)
entry = ttk.Entry(root, font=15, width=35)
entry.grid(row=0, column=0, columnspan=5)
# Number field -----------------------------------------------------------
num_pad_x = 2
num_pad_y = 5
sym_pad_y = 10
# Row 1 Numbers ----------------------------------------------------------
bt_1 = ttk.Button(root, text="1", command=input_calculator_1)
bt_1.grid(row=1, column=0, pady=num_pad_y, padx=num_pad_x)
bt_2 = ttk.Button(root, text="2", command=input_calculator_2)
bt_2.grid(row=1, column=1, pady=num_pad_y, padx=num_pad_x)
bt_3 = ttk.Button(root, text="3", command=input_calculator_3)
bt_3.grid(row=1, column=2, pady=num_pad_y, padx=num_pad_x)
# Row 2 Numbers ----------------------------------------------------------
bt_4 = ttk.Button(root, text="4", command=input_calculator_4)
bt_4.grid(row=2, column=0, pady=num_pad_y, padx=num_pad_x)
bt_5 = ttk.Button(root, text="5", command=input_calculator_5)
bt_5.grid(row=2, column=1, pady=num_pad_y, padx=num_pad_x)
bt_6 = ttk.Button(root, text="6", command=input_calculator_6)
bt_6.grid(row=2, column=2, pady=num_pad_y, padx=num_pad_x)
# Row 3 Numbers ----------------------------------------------------------
bt_7 = ttk.Button(root, text="7", command=input_calculator_7)
bt_7.grid(row=3, column=0, pady=num_pad_y, padx=num_pad_x)
bt_8 = ttk.Button(root, text="8", command=input_calculator_8)
bt_8.grid(row=3, column=1, pady=num_pad_y, padx=num_pad_x)
bt_9 = ttk.Button(root, text="9", command=input_calculator_9)
bt_9.grid(row=3, column=2, pady=num_pad_y, padx=num_pad_x)
# Row 4 Numbers ----------------------------------------------------------
bt_0 = ttk.Button(root, text="0", command=input_calculator_0)
bt_0.grid(row=4, column=0, pady=num_pad_y, padx=num_pad_x)
bt_p = ttk.Button(root, text=".", command=input_calculator_p)
bt_p.grid(row=4, column=1, pady=num_pad_y, padx=num_pad_x)
bt_e = ttk.Button(root, text="=", command=calculate)
bt_e.grid(row=4, column=2, pady=num_pad_y, padx=num_pad_x)
# Row 1 Symbols ----------------------------------------------------------
bt_plus = ttk.Button(root, text="+", command=input_calculator_plus)
bt_plus.grid(row=1, column=3, pady=num_pad_y, padx=num_pad_x)
bt_percent = ttk.Button(root, text="%", command=input_calculator_percent)
bt_percent.grid(row=1, column=4, pady=num_pad_y, padx=num_pad_x)
# Row 2 Symbols ----------------------------------------------------------
bt_minus = ttk.Button(root, text="-", command=input_calculator_minus)
bt_minus.grid(row=2, column=3, pady=num_pad_y, padx=num_pad_x)
bt_par_open = ttk.Button(root, text="(", command=input_calculator_par_open)
bt_par_open.grid(row=2, column=4, pady=num_pad_y, padx=num_pad_x)
# Row 3 Symbols ----------------------------------------------------------
bt_mult = ttk.Button(root, text="*", command=input_calculator_mult)
bt_mult.grid(row=3, column=3, pady=num_pad_y, padx=num_pad_x)
bt_par_close = ttk.Button(root, text=")", command=input_calculator_par_close)
bt_par_close.grid(row=3, column=4, pady=num_pad_y, padx=num_pad_x)
# Row 4 Symbols ----------------------------------------------------------
bt_div = ttk.Button(root, text="/", command=input_calculator_div)
bt_div.grid(row=4, column=3, pady=num_pad_y, padx=num_pad_x)
bt_clear = ttk.Button(root, text="C", command=clear)
bt_clear.grid(row=4, column=4, pady=num_pad_y, padx=num_pad_x)
root.mainloop()