-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
208 lines (175 loc) · 7.47 KB
/
calculator.py
File metadata and controls
208 lines (175 loc) · 7.47 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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 8 19:30:17 2025
Updated by Jarvis 😎
"""
import tkinter as tk
import math
class Calculator(tk.Tk):
def __init__(self):
super().__init__()
self.title("🧮 Calcuverse - Scientific Calculator")
self.geometry("420x600")
self.resizable(False, False)
self.expression = ""
self.input_text = tk.StringVar()
self.history = []
self.mode = tk.StringVar(value="Standard")
self.degree_mode = tk.BooleanVar(value=True)
self.dark_mode = tk.BooleanVar(value=True)
self.configure(bg="#1e1e1e") # Initial dark mode
self.create_widgets()
self.bind_keys()
self.update_mode()
def create_widgets(self):
# Mode and theme toggle
top_frame = tk.Frame(self, bg=self.bg_color())
top_frame.pack(pady=10)
tk.Radiobutton(top_frame, text="Standard", variable=self.mode, value="Standard", command=self.update_mode,
bg=self.bg_color(), fg=self.fg_color(), selectcolor=self.bg_color()).grid(row=0, column=0)
tk.Radiobutton(top_frame, text="Scientific", variable=self.mode, value="Scientific", command=self.update_mode,
bg=self.bg_color(), fg=self.fg_color(), selectcolor=self.bg_color()).grid(row=0, column=1)
tk.Checkbutton(top_frame, text="Dark Mode", variable=self.dark_mode, command=self.toggle_theme,
bg=self.bg_color(), fg=self.fg_color(), selectcolor=self.bg_color()).grid(row=0, column=2)
# Display
display_frame = tk.Frame(self, bg=self.bg_color())
display_frame.pack()
self.entry = tk.Entry(display_frame, textvariable=self.input_text, font=("Consolas", 22),
bd=10, relief="ridge", justify="right", bg=self.entry_bg(), fg=self.fg_color())
self.entry.grid(row=0, column=0, columnspan=5, ipadx=8, ipady=10)
self.entry.bind("<Return>", self.calculate)
self.entry.bind("<BackSpace>", self.backspace)
# History display
self.history_box = tk.Listbox(self, height=4, font=("Courier", 12), bg=self.bg_color(), fg="#8ef6e4")
self.history_box.pack(pady=5, fill="x")
self.history_box.insert(tk.END, "Welcome to Calcuverse!")
self.create_buttons()
def create_buttons(self):
button_frame = tk.Frame(self, bg=self.bg_color())
button_frame.pack()
standard_buttons = [
("7", "8", "9", "/", "C"),
("4", "5", "6", "*", "←"),
("1", "2", "3", "-", "("),
("0", ".", "=", "+", ")"),
]
scientific_buttons = [
("sin", "cos", "tan", "log", "√"),
("π", "e", "x²", "exp", "deg"),
]
self.buttons = {}
# Standard Buttons
for r, row in enumerate(standard_buttons):
for c, char in enumerate(row):
btn = tk.Button(button_frame, text=char, font=("Arial", 14), width=5, height=2,
bg=self.button_bg(), fg=self.fg_color(), activebackground="#444",
command=lambda ch=char: self.on_button_click(ch))
btn.grid(row=r, column=c, padx=1, pady=1)
self.buttons[char] = btn
# Scientific Buttons
self.sci_frame = tk.Frame(button_frame, bg=self.bg_color())
for r, row in enumerate(scientific_buttons):
for c, char in enumerate(row):
btn = tk.Button(self.sci_frame, text=char, font=("Arial", 14), width=5, height=2,
bg=self.button_bg(), fg=self.fg_color(), activebackground="#444",
command=lambda ch=char: self.on_button_click(ch))
btn.grid(row=r, column=c, padx=1, pady=1)
self.buttons[char] = btn
def update_mode(self):
if hasattr(self, 'sci_frame'):
if self.mode.get() == "Scientific":
self.sci_frame.grid(row=5, column=0, columnspan=5)
else:
self.sci_frame.grid_forget()
def toggle_theme(self):
bg = self.bg_color()
fg = self.fg_color()
self.configure(bg=bg)
for widget in self.winfo_children():
try:
widget.configure(bg=bg, fg=fg)
except:
pass
self.create_widgets()
def on_button_click(self, char):
if char == "=":
self.auto_close_brackets()
self.calculate()
elif char == "C":
self.expression = ""
elif char == "←":
self.expression = self.expression[:-1]
elif char == "π":
self.expression += str(math.pi)
elif char == "e":
self.expression += str(math.e)
elif char == "x²":
self.expression += "**2"
elif char == "√":
self.expression += "math.sqrt("
elif char == "log":
self.expression += "math.log10("
elif char == "sin":
self.expression += "math.sin(math.radians(" if self.degree_mode.get() else "math.sin("
elif char == "cos":
self.expression += "math.cos(math.radians(" if self.degree_mode.get() else "math.cos("
elif char == "tan":
self.expression += "math.tan(math.radians(" if self.degree_mode.get() else "math.tan("
elif char == "exp":
self.expression += "math.exp("
elif char == "deg":
self.degree_mode.set(not self.degree_mode.get())
else:
self.expression += char
self.input_text.set(self.expression)
def calculate(self, event=None):
try:
result = eval(self.expression, {"math": math})
result = round(result, 6)
self.history.append(f"{self.expression} = {result}")
self.update_history()
self.input_text.set(result)
self.expression = str(result)
except ZeroDivisionError:
self.input_text.set("Zero Division Error")
self.expression = ""
except SyntaxError:
self.input_text.set("Syntax Error")
self.expression = ""
except Exception as e:
self.input_text.set("Unexpected Error")
self.expression = ""
def auto_close_brackets(self):
open_count = self.expression.count("(")
close_count = self.expression.count(")")
while close_count < open_count:
self.expression += ")"
close_count += 1
def update_history(self):
self.history_box.delete(0, tk.END)
for item in reversed(self.history[-4:]):
self.history_box.insert(tk.END, item)
def backspace(self, event=None):
self.expression = self.expression[:-1]
self.input_text.set(self.expression)
def bind_keys(self):
self.bind("<Key>", self.handle_keypress)
def handle_keypress(self, event):
key = event.char
if key in "0123456789+-*/().":
self.on_button_click(key)
elif key == "\r":
self.calculate()
elif key == "\b":
self.backspace()
def bg_color(self):
return "#1e1e1e" if self.dark_mode.get() else "#f5f5f5"
def fg_color(self):
return "#ffffff" if self.dark_mode.get() else "#000000"
def entry_bg(self):
return "#3a3a3a" if self.dark_mode.get() else "#ffffff"
def button_bg(self):
return "#333" if self.dark_mode.get() else "#dcdcdc"
if __name__ == "__main__":
app = Calculator()
app.mainloop()