-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_calculator.py
More file actions
131 lines (118 loc) · 5.15 KB
/
gui_calculator.py
File metadata and controls
131 lines (118 loc) · 5.15 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
import tkinter as tk
from tkinter import messagebox
# Calculator logic
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Error"
return a / b
# GUI setup
class Calculator(tk.Tk):
def __init__(self):
super().__init__()
self.title("Python Calculator")
self.geometry("320x570")
self.resizable(False, False)
self.expression = ""
# ...existing code...
self.create_widgets()
def create_widgets(self):
# Color scheme
display_bg = "#222831"
display_fg = "#eeeeee"
display_font = ("Arial", 28, "bold")
button_bg = "#393e46"
button_fg = "#00adb5"
button_font = ("Arial", 20, "bold")
special_bg = "#00adb5"
special_fg = "#222831"
operator_bg = "#ff5722"
operator_fg = "#fff"
pad = 8
# Improved display box
self.display = tk.Entry(self, font=display_font, borderwidth=4, relief="groove", justify="right",
bg=display_bg, fg=display_fg)
self.display.pack(fill="x", padx=pad, pady=(pad, pad//2))
buttons = [
["7", "8", "9", "/"],
["4", "5", "6", "*"],
["1", "2", "3", "-"],
["0", ".", "=", "+"],
["C"],
["Restart", "Close"]
]
for row in buttons:
frame = tk.Frame(self, bg="", pady=pad//2)
frame.pack(expand=True, fill="both", padx=pad)
for btn in row:
# Custom button style
if btn == "Close":
bg = "#d7263d"
fg = "#fff"
b = tk.Button(frame, text=btn, font=button_font, command=self.destroy,
bg=bg, fg=fg, activebackground="#fff", activeforeground=bg,
relief="flat", borderwidth=0, highlightthickness=0)
elif btn == "Restart":
bg = "#f7b32b"
fg = "#222831"
b = tk.Button(frame, text=btn, font=button_font, command=self.restart_calculator,
bg=bg, fg=fg, activebackground="#fff", activeforeground=bg,
relief="flat", borderwidth=0, highlightthickness=0)
elif btn in ["=", "C"]:
bg = special_bg
fg = special_fg
b = tk.Button(frame, text=btn, font=button_font, command=lambda x=btn: self.on_button_click(x),
bg=bg, fg=fg, activebackground=button_bg, activeforeground=button_fg,
relief="flat", borderwidth=0, highlightthickness=0)
elif btn in ["/", "*", "-", "+"]:
bg = operator_bg
fg = operator_fg
b = tk.Button(frame, text=btn, font=button_font, command=lambda x=btn: self.on_button_click(x),
bg=bg, fg=fg, activebackground="#fff", activeforeground=bg,
relief="flat", borderwidth=0, highlightthickness=0)
else:
bg = button_bg
fg = button_fg
b = tk.Button(frame, text=btn, font=button_font, command=lambda x=btn: self.on_button_click(x),
bg=bg, fg=fg, activebackground=special_bg, activeforeground=special_fg,
relief="flat", borderwidth=0, highlightthickness=0)
b.configure(width=4, height=2)
b.pack(side="left", expand=True, fill="both", padx=(pad//2), pady=(pad//2))
def on_enter(e, btn=b, bg=bg, fg=fg):
btn.configure(bg=fg, fg=bg)
def on_leave(e, btn=b, bg=bg, fg=fg):
btn.configure(bg=bg, fg=fg)
b.bind("<Enter>", lambda e, btn=b, bg=bg, fg=fg: on_enter(e, btn, bg, fg))
b.bind("<Leave>", lambda e, btn=b, bg=bg, fg=fg: on_leave(e, btn, bg, fg))
# Add product label at the bottom
product_label = tk.Label(self, text="Product built by Turei Milner", font=("Arial", 10, "italic"), fg="#888", bg="#222831")
product_label.pack(side="bottom", fill="x", pady=(4, 8))
def restart_calculator(self):
self.expression = ""
self.display.delete(0, tk.END)
def on_button_click(self, char):
if char == "C":
self.expression = ""
self.display.delete(0, tk.END)
elif char == "=":
try:
result = eval(self.expression)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
self.expression = str(result)
except Exception:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
self.expression = ""
else:
self.expression += str(char)
self.display.delete(0, tk.END)
self.display.insert(tk.END, self.expression)
if __name__ == "__main__":
app = Calculator()
app.mainloop()