-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
111 lines (95 loc) · 3.01 KB
/
Copy pathcalculator.py
File metadata and controls
111 lines (95 loc) · 3.01 KB
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
import tkinter as tk
def calculater(oper):
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
except ValueError:
lbl_result.config(text="Invalid Input!", fg="#ff4d4d")
return
result = None
if oper == '+':
result = num1 + num2
elif oper == '-':
result = num1 - num2
elif oper == '*':
result = num1 * num2
elif oper == '/':
if num2 != 0:
result = num1 / num2
else:
lbl_result.config(text="Error: Zero Division", fg="#ff4d4d")
return
elif oper == '**':
result = num1 ** num2
elif oper == '%':
if num2 != 0:
result = num1 % num2
else:
lbl_result.config(text="Error: Zero Modulus!", fg="#ff4d4d")
return
if result is not None:
if result == int(result):
result = int(result)
lbl_result.config(text=f"Result: {result}", fg="#ff69b4")
def clear_f():
entry_num1.delete(0, tk.END)
entry_num2.delete(0, tk.END)
lbl_result.config(text="Result: -", fg="#ffffff")
window = tk.Tk()
window.title("Calculator")
window.geometry("320x420")
window.configure(bg="#121212")
window.resizable(False, False)
tk.Label(window, text="First Number:", bg="#121212", fg="#ff69b4", font=("Helvetica", 10, "bold")).pack(pady=(20, 2))
entry_num1 = tk.Entry(window, font=("Helvetica", 11), justify="center", bg="#1e1e1e", fg="#ffffff", insertbackground="white", bd=1, relief="solid")
entry_num1.pack(ipady=4, padx=20)
tk.Label(window, text="Second Number:", bg="#121212", fg="#ff69b4", font=("Helvetica", 10, "bold")).pack(pady=(12, 2))
entry_num2 = tk.Entry(window, font=("Helvetica", 11), justify="center", bg="#1e1e1e", fg="#ffffff", insertbackground="white", bd=1, relief="solid")
entry_num2.pack(ipady=4, padx=20)
frame_buttons = tk.Frame(window, bg="#121212")
frame_buttons.pack(pady=20)
operators = [
('+', '+'), ('-', '-'), ('*', '*'),
('/', '/'), ('**', '**'), ('%', '%')
]
for i, (text, oper) in enumerate(operators):
row = i // 3
col = i % 3
btn = tk.Button(
frame_buttons,
text=text,
width=5,
height=1,
font=("Helvetica", 10, "bold"),
bg="#ff69b4",
fg="#121212",
activebackground="#ff1493",
activeforeground="#ffffff",
bd=0,
cursor="hand2",
command=lambda op=oper: calculater(op)
)
btn.grid(row=row, column=col, padx=6, pady=6)
btn_clear = tk.Button(
window,
text="Clear",
width=18,
bg="#333333",
fg="#ff69b4",
font=("Helvetica", 9, "bold"),
activebackground="#ff4d4d",
activeforeground="#ffffff",
bd=0,
cursor="hand2",
command=clear_f
)
btn_clear.pack(pady=5)
lbl_result = tk.Label(
window,
text="Result: -",
bg="#121212",
fg="#ffffff",
font=("Helvetica", 12, "bold")
)
lbl_result.pack(pady=15)
window.mainloop()