-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathAk CAlculator
More file actions
153 lines (142 loc) · 5.68 KB
/
Ak CAlculator
File metadata and controls
153 lines (142 loc) · 5.68 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
import tkinter as tk
from tkinter import messagebox
import math
class AKCalculator:
def __init__(self, root):
self.root = root
self.root.title("AK Calculator")
self.root.geometry("400x600")
self.root.resizable(True, True)
self.theme = "light"
self.create_widgets()
self.bind_keys()
def create_widgets(self):
self.display = tk.Entry(self.root, font=("Arial", 30), borderwidth=2, relief="solid")
self.display.grid(row=0, column=0, columnspan=4, sticky="nsew")
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'C', '√', 'x²', 'x³',
'sin', 'cos', 'tan', 'log',
'!', '%', '|x|', 'x^y',
'Light/Dark'
]
row_val = 1
col_val = 0
for button in buttons:
action = lambda x=button: self.click_event(x)
tk.Button(self.root, text=button, width=5, height=2, command=action).grid(row=row_val, column=col_val, sticky="nsew")
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
for i in range(5):
self.root.grid_rowconfigure(i, weight=1)
self.root.grid_columnconfigure(i, weight=1)
def bind_keys(self):
self.root.bind("<Return>", lambda event: self.click_event('='))
self.root.bind("<BackSpace>", lambda event: self.click_event('C'))
for key in '0123456789+-*/.':
self.root.bind(key, lambda event, char=key: self.click_event(char))
def click_event(self, key):
if key == '=':
try:
result = str(eval(self.display.get()))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'C':
self.display.delete(0, tk.END)
elif key == '√':
try:
result = str(math.sqrt(float(self.display.get())))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'x²':
try:
result = str(float(self.display.get()) ** 2)
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'x³':
try:
result = str(float(self.display.get()) ** 3)
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'sin':
try:
result = str(math.sin(math.radians(float(self.display.get()))))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'cos':
try:
result = str(math.cos(math.radians(float(self.display.get()))))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'tan':
try:
result = str(math.tan(math.radians(float(self.display.get()))))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'log':
try:
result = str(math.log10(float(self.display.get())))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == '!':
try:
result = str(math.factorial(int(self.display.get())))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == '%':
try:
result = str(float(self.display.get()) / 100)
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == '|x|':
try:
result = str(abs(float(self.display.get())))
self.display.delete(0, tk.END)
self.display.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid Input")
elif key == 'x^y':
self.display.insert(tk.END, '**')
elif key == 'Light/Dark':
self.toggle_theme()
else:
self.display.insert(tk.END, key)
def toggle_theme(self):
if self.theme == "light":
self.root.configure(bg="black")
self.display.configure(bg="black", fg="white")
self.theme = "dark"
else:
self.root.configure(bg="white")
self.display.configure(bg="white", fg="black")
self.theme = "light"
if __name__ == "__main__":
root = tk.Tk()
calculator = AKCalculator(root)
pip install kivy
root.mainloop()wsl --install