Skip to content

Commit 77305a1

Browse files
Merge pull request Gagandeep-2003#65 from 6vam4arya/feat/toggle-button
✨ Feature: Dark/Light Mode Toggle Button for GUI
2 parents eb5dc4e + fcec4ce commit 77305a1

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

main.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from tkinter import ttk, messagebox
33
import subprocess
44

5-
face_proc = None
5+
face_proc = None
6+
is_dark_mode = False # Tracks current theme state
67

78
def run_face_detection():
89
global face_proc
@@ -11,40 +12,74 @@ def run_face_detection():
1112
except Exception as e:
1213
messagebox.showerror("Error", f"Failed to run face detection:\n{e}")
1314

14-
1515
def run_blink_detection():
1616
try:
1717
subprocess.call(["python", "blinkDetect.py"])
1818
except Exception as e:
1919
messagebox.showerror("Error", f"Failed to run blink detection:\n{e}")
2020

21+
def toggle_theme(root, frame, toggle_btn):
22+
global is_dark_mode
23+
24+
if is_dark_mode:
25+
# Switch to light mode
26+
root.configure(bg="#f0f0f0")
27+
frame.configure(style="Light.TFrame")
28+
toggle_btn.config(text="Switch to Dark Mode")
29+
ttk.Style().configure('TButton', background="#ffffff", foreground="#000000")
30+
else:
31+
# Switch to dark mode
32+
root.configure(bg="#2e2e2e")
33+
frame.configure(style="Dark.TFrame")
34+
toggle_btn.config(text="Switch to Light Mode")
35+
ttk.Style().configure('TButton', background="#444444", foreground="#ffffff")
36+
37+
is_dark_mode = not is_dark_mode
38+
2139
def on_quit(root):
2240
if face_proc and face_proc.poll() is None:
2341
face_proc.terminate()
2442
root.destroy()
2543

26-
2744
def main():
2845
root = tk.Tk()
2946
root.title("Driver Drowsiness Detection System")
3047
root.geometry("500x500")
48+
root.configure(bg="#f0f0f0") # Default light background
3149

3250
style = ttk.Style()
33-
style.configure('TButton', font=('Calibri', 20, 'bold'), borderwidth=2)
51+
style.theme_use("clam")
52+
53+
# Frame styles
54+
style.configure("Light.TFrame", background="#f0f0f0")
55+
style.configure("Dark.TFrame", background="#2e2e2e")
3456

35-
frame = ttk.Frame(root, padding=20)
57+
# Button styles
58+
style.configure('TButton',
59+
font=('Segoe UI', 14, 'bold'),
60+
padding=10,
61+
borderwidth=1,
62+
relief="raised")
63+
64+
frame = ttk.Frame(root, padding=20, style="Light.TFrame")
3665
frame.pack(expand=True)
3766

3867
btn_face = ttk.Button(frame, text="Face Detection", command=run_face_detection)
39-
btn_face.pack(side=tk.LEFT, padx=10, pady=10)
68+
btn_face.grid(row=0, column=0, padx=15, pady=15)
4069

4170
btn_blink = ttk.Button(frame, text="Blink Detection", command=run_blink_detection)
42-
btn_blink.pack(side=tk.RIGHT, padx=10, pady=10)
71+
btn_blink.grid(row=0, column=1, padx=15, pady=15)
72+
73+
# Toggle button
74+
btn_toggle = ttk.Button(root, text="Switch to Dark Mode")
75+
btn_toggle.config(command=lambda: toggle_theme(root, frame, btn_toggle))
76+
btn_toggle.pack(pady=10)
4377

78+
# Quit button
4479
btn_quit = ttk.Button(root, text="Quit", command=lambda: on_quit(root))
4580
btn_quit.pack(side=tk.BOTTOM, pady=20)
4681

4782
root.mainloop()
4883

4984
if __name__ == "__main__":
50-
main()
85+
main()

0 commit comments

Comments
 (0)