Add theme management and settings functionality
Build and Push Docker Image / build-and-push (push) Has been cancelled
Build and Push Docker Image / build-and-push (push) Has been cancelled
- Introduced `ThemeManager` to handle application themes using `ttkthemes`. - Added `SettingsWindow` for user preferences including theme selection and UI settings. - Integrated theme selection into the main application with a menu for quick access. - Enhanced UI components with custom styles based on the selected theme. - Implemented tooltips for better user guidance across various UI elements. - Updated dependencies to include `ttkthemes` for improved visual appeal.
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
"""Theme manager for the application using ttkthemes."""
|
||||
|
||||
import logging
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
|
||||
from ttkthemes import ThemedStyle
|
||||
|
||||
|
||||
class ThemeManager:
|
||||
"""Manages application themes and styling."""
|
||||
|
||||
def __init__(self, root: tk.Tk, logger: logging.Logger) -> None:
|
||||
self.root = root
|
||||
self.logger = logger
|
||||
self.style: ThemedStyle | None = None
|
||||
self.current_theme: str = "arc" # Default theme
|
||||
|
||||
# Available themes - these are some of the best looking ones
|
||||
self.available_themes = [
|
||||
"arc",
|
||||
"equilux",
|
||||
"adapta",
|
||||
"yaru",
|
||||
"ubuntu",
|
||||
"plastik",
|
||||
"breeze",
|
||||
"elegance",
|
||||
]
|
||||
|
||||
self.initialize_theme()
|
||||
|
||||
def initialize_theme(self) -> None:
|
||||
"""Initialize the themed style."""
|
||||
try:
|
||||
self.style = ThemedStyle(self.root)
|
||||
self.apply_theme(self.current_theme)
|
||||
self._configure_custom_styles()
|
||||
self.logger.info(
|
||||
f"Theme manager initialized with theme: {self.current_theme}"
|
||||
)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to initialize theme manager: {e}")
|
||||
# Fallback to default ttk styling
|
||||
self.style = ttk.Style()
|
||||
|
||||
def apply_theme(self, theme_name: str) -> bool:
|
||||
"""Apply a specific theme."""
|
||||
try:
|
||||
if self.style and theme_name in self.get_available_themes():
|
||||
self.style.set_theme(theme_name)
|
||||
self.current_theme = theme_name
|
||||
self._configure_custom_styles()
|
||||
self.logger.info(f"Applied theme: {theme_name}")
|
||||
return True
|
||||
else:
|
||||
self.logger.warning(f"Theme '{theme_name}' not available")
|
||||
return False
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to apply theme '{theme_name}': {e}")
|
||||
return False
|
||||
|
||||
def get_available_themes(self) -> list[str]:
|
||||
"""Get list of available themes."""
|
||||
if self.style:
|
||||
try:
|
||||
# Get all available themes from ttkthemes
|
||||
all_themes = self.style.theme_names()
|
||||
# Filter to only include our curated list
|
||||
return [theme for theme in self.available_themes if theme in all_themes]
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to get available themes: {e}")
|
||||
return self.available_themes
|
||||
return self.available_themes
|
||||
|
||||
def get_current_theme(self) -> str:
|
||||
"""Get the currently active theme."""
|
||||
return self.current_theme
|
||||
|
||||
def _configure_custom_styles(self) -> None:
|
||||
"""Configure custom styles for better appearance."""
|
||||
if not self.style:
|
||||
return
|
||||
|
||||
try:
|
||||
# Get current theme colors for consistent styling
|
||||
colors = self.get_theme_colors()
|
||||
|
||||
# Configure frame styles with better padding and borders
|
||||
self.style.configure(
|
||||
"Card.TFrame",
|
||||
relief="flat",
|
||||
borderwidth=0,
|
||||
background=colors["bg"],
|
||||
)
|
||||
|
||||
# Configure label frame styles with modern appearance
|
||||
self.style.configure(
|
||||
"Card.TLabelframe",
|
||||
relief="solid",
|
||||
borderwidth=1,
|
||||
background=colors["bg"],
|
||||
foreground=colors["fg"],
|
||||
padding=(10, 5, 10, 10),
|
||||
)
|
||||
|
||||
self.style.configure(
|
||||
"Card.TLabelframe.Label",
|
||||
background=colors["bg"],
|
||||
foreground=colors["fg"],
|
||||
font=("TkDefaultFont", 10, "bold"),
|
||||
)
|
||||
|
||||
# Configure button styles for better appearance
|
||||
self.style.configure(
|
||||
"Action.TButton",
|
||||
padding=(15, 8),
|
||||
font=("TkDefaultFont", 9, "normal"),
|
||||
)
|
||||
|
||||
# Configure entry styles with modern look
|
||||
self.style.configure(
|
||||
"Modern.TEntry",
|
||||
padding=(8, 5),
|
||||
borderwidth=1,
|
||||
relief="solid",
|
||||
)
|
||||
|
||||
# Configure scale styles for pathology inputs
|
||||
self.style.configure(
|
||||
"Modern.Horizontal.TScale",
|
||||
borderwidth=0,
|
||||
background=colors["bg"],
|
||||
troughcolor="#e0e0e0",
|
||||
lightcolor=colors["select_bg"],
|
||||
darkcolor=colors["select_bg"],
|
||||
focuscolor=colors["select_bg"],
|
||||
)
|
||||
|
||||
# Configure treeview for better data display
|
||||
self.style.configure(
|
||||
"Modern.Treeview",
|
||||
rowheight=28,
|
||||
borderwidth=1,
|
||||
relief="solid",
|
||||
background=colors["bg"],
|
||||
foreground=colors["fg"],
|
||||
fieldbackground=colors["bg"],
|
||||
selectbackground=colors["select_bg"],
|
||||
selectforeground=colors["select_fg"],
|
||||
)
|
||||
|
||||
self.style.configure(
|
||||
"Modern.Treeview.Heading",
|
||||
padding=(8, 6),
|
||||
relief="flat",
|
||||
borderwidth=1,
|
||||
background=colors["select_bg"],
|
||||
foreground=colors["select_fg"],
|
||||
font=("TkDefaultFont", 9, "bold"),
|
||||
)
|
||||
|
||||
# Configure comprehensive row selection colors for better visibility
|
||||
self.style.map(
|
||||
"Modern.Treeview",
|
||||
background=[
|
||||
("selected", colors["select_bg"]),
|
||||
("active", colors["select_bg"]),
|
||||
("focus", colors["select_bg"]),
|
||||
("", colors["bg"]),
|
||||
],
|
||||
foreground=[
|
||||
("selected", colors["select_fg"]),
|
||||
("active", colors["select_fg"]),
|
||||
("focus", colors["select_fg"]),
|
||||
("", colors["fg"]),
|
||||
],
|
||||
selectbackground=[
|
||||
("focus", colors["select_bg"]),
|
||||
("", colors["select_bg"]),
|
||||
],
|
||||
selectforeground=[
|
||||
("focus", colors["select_fg"]),
|
||||
("", colors["select_fg"]),
|
||||
],
|
||||
)
|
||||
|
||||
# Configure notebook tabs with modern styling
|
||||
self.style.configure(
|
||||
"Modern.TNotebook.Tab",
|
||||
padding=(15, 8),
|
||||
borderwidth=1,
|
||||
relief="flat",
|
||||
)
|
||||
|
||||
self.style.map(
|
||||
"Modern.TNotebook.Tab",
|
||||
background=[("selected", colors["select_bg"])],
|
||||
foreground=[("selected", colors["select_fg"])],
|
||||
)
|
||||
|
||||
# Configure checkbutton for medicine selection
|
||||
self.style.configure(
|
||||
"Modern.TCheckbutton",
|
||||
padding=(8, 4),
|
||||
background=colors["bg"],
|
||||
foreground=colors["fg"],
|
||||
focuscolor=colors["select_bg"],
|
||||
)
|
||||
|
||||
self.logger.debug("Enhanced custom styles configured")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to configure custom styles: {e}")
|
||||
|
||||
def configure_widget_style(self, widget: tk.Widget, style_name: str) -> None:
|
||||
"""Apply a specific style to a widget."""
|
||||
try:
|
||||
if hasattr(widget, "configure") and self.style:
|
||||
widget.configure(style=style_name)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to configure widget style '{style_name}': {e}")
|
||||
|
||||
def get_theme_colors(self) -> dict[str, str]:
|
||||
"""Get current theme colors for custom widgets."""
|
||||
if not self.style:
|
||||
return {
|
||||
"bg": "#ffffff",
|
||||
"fg": "#000000",
|
||||
"select_bg": "#3584e4",
|
||||
"select_fg": "#ffffff",
|
||||
"alt_bg": "#f5f5f5",
|
||||
}
|
||||
|
||||
try:
|
||||
# Get colors from current theme
|
||||
bg = self.style.lookup("TFrame", "background") or "#ffffff"
|
||||
fg = self.style.lookup("TLabel", "foreground") or "#000000"
|
||||
|
||||
# Try to get better selection colors from different widget states
|
||||
select_bg = (
|
||||
self.style.lookup("TButton", "background", ["pressed"])
|
||||
or self.style.lookup("TButton", "background", ["active"])
|
||||
or self.style.lookup("Treeview", "selectbackground")
|
||||
or "#0078d4" # Modern blue fallback
|
||||
)
|
||||
select_fg = (
|
||||
self.style.lookup("TButton", "foreground", ["pressed"])
|
||||
or self.style.lookup("TButton", "foreground", ["active"])
|
||||
or self.style.lookup("Treeview", "selectforeground")
|
||||
or "#ffffff" # White fallback
|
||||
)
|
||||
|
||||
# Ensure contrast - if selection colors are too similar to background,
|
||||
# use fallbacks
|
||||
if select_bg == bg or select_bg.lower() == bg.lower():
|
||||
select_bg = "#0078d4" if bg != "#0078d4" else "#0066cc"
|
||||
|
||||
if select_fg == fg or select_fg.lower() == fg.lower():
|
||||
select_fg = "#ffffff" if fg != "#ffffff" else "#000000"
|
||||
|
||||
# Calculate alternating row color
|
||||
if bg.startswith("#"):
|
||||
try:
|
||||
rgb = tuple(int(bg[i : i + 2], 16) for i in (1, 3, 5))
|
||||
if sum(rgb) > 384: # Light theme
|
||||
alt_bg = (
|
||||
f"#{max(0, rgb[0] - 10):02x}"
|
||||
f"{max(0, rgb[1] - 10):02x}"
|
||||
f"{max(0, rgb[2] - 10):02x}"
|
||||
)
|
||||
else: # Dark theme
|
||||
alt_bg = (
|
||||
f"#{min(255, rgb[0] + 10):02x}"
|
||||
f"{min(255, rgb[1] + 10):02x}"
|
||||
f"{min(255, rgb[2] + 10):02x}"
|
||||
)
|
||||
except ValueError:
|
||||
alt_bg = "#f5f5f5"
|
||||
else:
|
||||
alt_bg = "#f5f5f5"
|
||||
|
||||
return {
|
||||
"bg": bg,
|
||||
"fg": fg,
|
||||
"select_bg": select_bg,
|
||||
"select_fg": select_fg,
|
||||
"alt_bg": alt_bg, # Add alternating background color
|
||||
}
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to get theme colors: {e}")
|
||||
return {
|
||||
"bg": "#ffffff",
|
||||
"fg": "#000000",
|
||||
"select_bg": "#3584e4",
|
||||
"select_fg": "#ffffff",
|
||||
"alt_bg": "#f5f5f5",
|
||||
}
|
||||
Reference in New Issue
Block a user