-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkalkulator.py
More file actions
185 lines (153 loc) · 6.97 KB
/
kalkulator.py
File metadata and controls
185 lines (153 loc) · 6.97 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
import tkinter as tk
class CalculatorApp:
def __init__(self, root):
self.root = root
self.root.title("Kalkulator")
self.root.geometry("350x540")
self.root.resizable(False, False)
self.is_dark_mode = True
self.buttons = [] # Store button references to update colors
self.expression = ""
self.input_text = tk.StringVar()
# Theme Colors
self.colors = {
"dark": {
"bg": "#202020", "display_bg": "#303030", "text": "white",
"btn_num": "#505050", "btn_op": "#ff9500", "btn_action": "#a5a5a5",
"btn_num_text": "white", "btn_op_text": "white", "btn_action_text": "black"
},
"light": {
"bg": "#f0f0f0", "display_bg": "#e0e0e0", "text": "black",
"btn_num": "#ffffff", "btn_op": "#ff9500", "btn_action": "#d4d4d2",
"btn_num_text": "black", "btn_op_text": "white", "btn_action_text": "black"
}
}
# Top Bar for Theme Toggle
self.top_frame = tk.Frame(self.root)
self.top_frame.pack(side=tk.TOP, fill=tk.X, padx=10, pady=10)
# Icon only button, adjusted font size and padding for better centering
self.theme_btn = tk.Button(self.top_frame, text="☀", font=('Arial', 12),
command=self.toggle_theme, bd=1, relief="raised", cursor="hand2")
self.theme_btn.pack(side=tk.RIGHT, ipadx=10, ipady=2)
# Display Screen
self.input_frame = self.create_display()
self.input_frame.pack(side=tk.TOP, fill=tk.BOTH)
# Buttons
self.btns_frame = self.create_buttons()
self.btns_frame.pack(fill=tk.BOTH, expand=True)
# Apply initial theme
self.apply_theme()
def create_display(self):
frame = tk.Frame(self.root, height=100)
frame.pack_propagate(0) # Prevent frame from shrinking
vcmd = (self.root.register(self.validate_input), '%P')
self.entry = tk.Entry(frame, font=('Arial', 30, 'bold'), textvariable=self.input_text, width=50,
bd=0, justify=tk.RIGHT,
validate="key", validatecommand=vcmd)
self.entry.pack(side=tk.TOP, ipady=15, padx=10, pady=10, fill=tk.BOTH, expand=True)
return frame
def validate_input(self, new_value):
if new_value == "Error":
return True
allowed_chars = "0123456789.-+*/%() "
return all(char in allowed_chars for char in new_value)
def create_buttons(self):
frame = tk.Frame(self.root)
# Configure Grid Weights
for i in range(5):
frame.rowconfigure(i, weight=1)
for i in range(4):
frame.columnconfigure(i, weight=1)
# Helper to categorize buttons for theming
# types: 'num', 'op', 'action'
# Row 1
self.create_btn(frame, "C", 0, 0, 'action')
self.create_btn(frame, "⌫", 0, 1, 'action')
self.create_btn(frame, "%", 0, 2, 'action')
self.create_btn(frame, "/", 0, 3, 'op')
# Row 2
self.create_btn(frame, "7", 1, 0, 'num')
self.create_btn(frame, "8", 1, 1, 'num')
self.create_btn(frame, "9", 1, 2, 'num')
self.create_btn(frame, "*", 1, 3, 'op')
# Row 3
self.create_btn(frame, "4", 2, 0, 'num')
self.create_btn(frame, "5", 2, 1, 'num')
self.create_btn(frame, "6", 2, 2, 'num')
self.create_btn(frame, "-", 2, 3, 'op')
# Row 4
self.create_btn(frame, "1", 3, 0, 'num')
self.create_btn(frame, "2", 3, 1, 'num')
self.create_btn(frame, "3", 3, 2, 'num')
self.create_btn(frame, "+", 3, 3, 'op')
# Row 5 (0 spans 2 cols, . and =)
btn_0 = tk.Button(frame, text="0", font=('Arial', 18), bd=2, relief="raised",
command=lambda: self.on_button_click("0"))
btn_0.grid(row=4, column=0, columnspan=2, padx=2, pady=2, sticky="nsew")
self.buttons.append((btn_0, 'num'))
self.create_btn(frame, ".", 4, 2, 'num')
self.create_btn(frame, "=", 4, 3, 'op')
return frame
def create_btn(self, frame, text, row, col, btn_type):
btn = tk.Button(frame, text=text, font=('Arial', 18), bd=2, relief="raised",
command=lambda: self.on_button_click(text))
btn.grid(row=row, column=col, padx=2, pady=2, sticky="nsew")
self.buttons.append((btn, btn_type))
def toggle_theme(self):
self.is_dark_mode = not self.is_dark_mode
self.apply_theme()
def apply_theme(self):
theme = self.colors["dark" if self.is_dark_mode else "light"]
self.root.configure(bg=theme["bg"])
self.top_frame.configure(bg=theme["bg"])
self.input_frame.configure(bg=theme["bg"])
self.btns_frame.configure(bg=theme["bg"])
self.entry.configure(bg=theme["display_bg"], fg=theme["text"], insertbackground=theme["text"])
# Update Toggle Button: Icon only
# If Dark Mode -> Show Sun (to switch to light)
# If Light Mode -> Show Moon (to switch to dark)
self.theme_btn.configure(text="☀" if self.is_dark_mode else "☾",
bg=theme["btn_action"], fg=theme["btn_action_text"])
for btn, btn_type in self.buttons:
if btn_type == 'num':
bg = theme["btn_num"]
fg = theme["btn_num_text"]
elif btn_type == 'op':
bg = theme["btn_op"]
fg = theme["btn_op_text"]
else: # action
bg = theme["btn_action"]
fg = theme["btn_action_text"]
btn.configure(bg=bg, fg=fg, activebackground=theme["bg"], activeforeground=fg)
def on_button_click(self, char):
if char == "C":
self.expression = ""
self.input_text.set("")
elif char == "⌫":
self.expression = self.expression[:-1]
self.input_text.set(self.expression)
elif char == "=":
try:
# Replace visual symbols with python operators if needed (none used here yet except %)
# simple eval with error handling
result = str(eval(self.expression))
self.input_text.set(result)
self.expression = result
except:
self.input_text.set("Error")
self.expression = ""
elif char == "%":
try:
result = str(eval(self.expression) / 100)
self.input_text.set(result)
self.expression = result
except:
self.input_text.set("Error")
self.expression = ""
else:
self.expression += str(char)
self.input_text.set(self.expression)
if __name__ == "__main__":
root = tk.Tk()
app = CalculatorApp(root)
root.mainloop()