-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththeme_manager.py
More file actions
307 lines (258 loc) · 11.2 KB
/
Copy paththeme_manager.py
File metadata and controls
307 lines (258 loc) · 11.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
Theme Manager for NoSQL2SQL
Provides dark/light theme colors and applies them to tkinter widgets
"""
import tkinter as tk
from tkinter import ttk
class ThemeManager:
"""Manages application theming"""
# Light Theme Colors
LIGHT_THEME = {
"bg": "#FFFFFF",
"fg": "#000000",
"bg_secondary": "#F5F5F5",
"fg_secondary": "#666666",
"accent": "#007AFF",
"accent_hover": "#0051D5",
"success": "#34C759",
"error": "#FF3B30",
"warning": "#FF9500",
"border": "#D1D1D6",
"input_bg": "#FFFFFF",
"input_fg": "#000000",
"button_bg": "#007AFF",
"button_fg": "#FFFFFF",
"disabled": "#C7C7CC",
"selection": "#007AFF",
"text_bg": "#FFFFFF",
"text_fg": "#000000",
"heading": "#000000",
"link": "#007AFF"
}
# Dark Theme Colors
DARK_THEME = {
"bg": "#1C1C1E",
"fg": "#FFFFFF",
"bg_secondary": "#2C2C2E",
"fg_secondary": "#ABABAB",
"accent": "#0A84FF",
"accent_hover": "#409CFF",
"success": "#30D158",
"error": "#FF453A",
"warning": "#FF9F0A",
"border": "#38383A",
"input_bg": "#2C2C2E",
"input_fg": "#FFFFFF",
"button_bg": "#0A84FF",
"button_fg": "#FFFFFF",
"disabled": "#48484A",
"selection": "#0A84FF",
"text_bg": "#2C2C2E",
"text_fg": "#FFFFFF",
"heading": "#FFFFFF",
"link": "#0A84FF"
}
def __init__(self, root, theme_name="light"):
self.root = root
self.theme_name = theme_name
self.colors = self.LIGHT_THEME if theme_name == "light" else self.DARK_THEME
self.style = ttk.Style(root)
self._setup_style()
def _setup_style(self):
"""Setup ttk styles based on theme"""
# Configure root window
self.root.configure(bg=self.colors["bg"])
# Base ttk style
self.style.theme_use('clam')
# Configure basic widgets
self.style.configure(".",
background=self.colors["bg"],
foreground=self.colors["fg"],
bordercolor=self.colors["border"],
darkcolor=self.colors["bg_secondary"],
lightcolor=self.colors["bg"],
troughcolor=self.colors["bg_secondary"],
focuscolor=self.colors["accent"],
selectbackground=self.colors["selection"],
selectforeground=self.colors["button_fg"])
# Frame
self.style.configure("TFrame",
background=self.colors["bg"])
# Label
self.style.configure("TLabel",
background=self.colors["bg"],
foreground=self.colors["fg"])
self.style.configure("Heading.TLabel",
background=self.colors["bg"],
foreground=self.colors["heading"],
font=("Arial", 12, "bold"))
self.style.configure("Secondary.TLabel",
background=self.colors["bg"],
foreground=self.colors["fg_secondary"],
font=("Arial", 9))
# Button
self.style.configure("TButton",
background=self.colors["button_bg"],
foreground=self.colors["button_fg"],
bordercolor=self.colors["border"],
focuscolor=self.colors["accent"],
padding=(10, 5))
self.style.map("TButton",
background=[("active", self.colors["accent_hover"]),
("disabled", self.colors["disabled"])])
self.style.configure("Accent.TButton",
background=self.colors["accent"],
foreground=self.colors["button_fg"],
font=("Arial", 10, "bold"))
self.style.map("Accent.TButton",
background=[("active", self.colors["accent_hover"])])
# Entry
self.style.configure("TEntry",
fieldbackground=self.colors["input_bg"],
foreground=self.colors["input_fg"],
bordercolor=self.colors["border"],
insertcolor=self.colors["fg"])
# Combobox
self.style.configure("TCombobox",
fieldbackground=self.colors["input_bg"],
background=self.colors["bg"],
foreground=self.colors["input_fg"],
bordercolor=self.colors["border"],
arrowcolor=self.colors["fg"],
selectbackground=self.colors["selection"],
selectforeground=self.colors["button_fg"])
self.style.map("TCombobox",
fieldbackground=[("readonly", self.colors["input_bg"])],
selectbackground=[("readonly", self.colors["input_bg"])])
# Checkbutton
self.style.configure("TCheckbutton",
background=self.colors["bg"],
foreground=self.colors["fg"])
# Radiobutton
self.style.configure("TRadiobutton",
background=self.colors["bg"],
foreground=self.colors["fg"])
# LabelFrame
self.style.configure("TLabelframe",
background=self.colors["bg"],
bordercolor=self.colors["border"])
self.style.configure("TLabelframe.Label",
background=self.colors["bg"],
foreground=self.colors["fg"],
font=("Arial", 10, "bold"))
# Notebook (Tabs)
self.style.configure("TNotebook",
background=self.colors["bg"],
bordercolor=self.colors["border"])
self.style.configure("TNotebook.Tab",
background=self.colors["bg_secondary"],
foreground=self.colors["fg"],
padding=(10, 5))
self.style.map("TNotebook.Tab",
background=[("selected", self.colors["bg"])],
foreground=[("selected", self.colors["accent"])])
# Progressbar
self.style.configure("TProgressbar",
background=self.colors["accent"],
troughcolor=self.colors["bg_secondary"],
bordercolor=self.colors["border"],
lightcolor=self.colors["accent"],
darkcolor=self.colors["accent"])
# Scrollbar
self.style.configure("TScrollbar",
background=self.colors["bg_secondary"],
troughcolor=self.colors["bg"],
bordercolor=self.colors["border"],
arrowcolor=self.colors["fg"])
self.style.map("TScrollbar",
background=[("active", self.colors["border"])])
# Separator
self.style.configure("TSeparator",
background=self.colors["border"])
def switch_theme(self, theme_name):
"""Switch to a different theme"""
self.theme_name = theme_name
self.colors = self.LIGHT_THEME if theme_name == "light" else self.DARK_THEME
self._setup_style()
def get_color(self, color_name):
"""Get a specific color from current theme"""
return self.colors.get(color_name, "#000000")
def apply_to_text_widget(self, text_widget):
"""Apply theme to a Text widget"""
text_widget.configure(
bg=self.colors["text_bg"],
fg=self.colors["text_fg"],
insertbackground=self.colors["fg"],
selectbackground=self.colors["selection"],
selectforeground=self.colors["button_fg"]
)
def apply_to_listbox(self, listbox):
"""Apply theme to a Listbox widget"""
listbox.configure(
bg=self.colors["text_bg"],
fg=self.colors["text_fg"],
selectbackground=self.colors["selection"],
selectforeground=self.colors["button_fg"]
)
def apply_to_menu(self, menu):
"""Apply theme to a Menu widget"""
menu.configure(
bg=self.colors["bg"],
fg=self.colors["fg"],
activebackground=self.colors["accent"],
activeforeground=self.colors["button_fg"],
borderwidth=1,
relief=tk.FLAT
)
def apply_to_window(self, window):
"""Apply current theme to a Toplevel window"""
window.configure(bg=self.colors["bg"])
# Apply to all child widgets recursively
self._apply_to_widget_tree(window)
def _apply_to_widget_tree(self, widget):
"""Recursively apply theme to widget and its children"""
# Apply to current widget
try:
if isinstance(widget, (tk.Text, tk.Entry)):
widget.configure(
bg=self.colors["text_bg"],
fg=self.colors["text_fg"],
insertbackground=self.colors["fg"],
selectbackground=self.colors["selection"],
selectforeground=self.colors["button_fg"]
)
elif isinstance(widget, tk.Frame):
widget.configure(bg=self.colors["bg"])
elif isinstance(widget, tk.Label):
widget.configure(
bg=self.colors["bg"],
fg=self.colors["fg"]
)
elif isinstance(widget, tk.Toplevel):
widget.configure(bg=self.colors["bg"])
except:
pass # Some widgets don't support all options
# Recursively apply to children
try:
for child in widget.winfo_children():
self._apply_to_widget_tree(child)
except:
pass
if __name__ == "__main__":
# Test the theme manager
root = tk.Tk()
root.title("Theme Manager Test")
root.geometry("400x300")
theme_manager = ThemeManager(root, "dark")
frame = ttk.Frame(root, padding="20")
frame.pack(fill=tk.BOTH, expand=True)
ttk.Label(frame, text="Theme Manager Test", style="Heading.TLabel").pack(pady=5)
ttk.Label(frame, text="This is a test label").pack(pady=5)
ttk.Button(frame, text="Test Button").pack(pady=5)
ttk.Entry(frame).pack(pady=5)
def toggle_theme():
current = theme_manager.theme_name
new_theme = "light" if current == "dark" else "dark"
theme_manager.switch_theme(new_theme)
ttk.Button(frame, text="Toggle Theme", command=toggle_theme).pack(pady=10)
root.mainloop()