yahtzee-py/views/theme.py

69 lines
2.7 KiB
Python
Raw Permalink Normal View History

2026-11-02 21:43:42 +00:00
import tkinter as tk
import tkinter.font as tkfont
BG_COLOR = "#111018"
FG_COLOR = "#E0E0E0"
ACCENT_ORANGE = "#FF7518"
class DynamicImgButton(tk.Button):
2026-11-03 21:21:12 +00:00
"""Ein Bild-Button, dessen Hintergrundgrafik automatisch mit Textlänge und Höhe skaliert."""
def __init__(self, parent, text, img, img_active, command, min_px_width=None, height=None, font_size=11):
2026-11-02 21:43:42 +00:00
self._assets = getattr(parent.winfo_toplevel(), "assets", None)
2026-11-03 21:21:12 +00:00
self._font = tkfont.Font(family="Helvetica", size=font_size, weight="bold")
2026-11-02 21:43:42 +00:00
self._min_px_width = min_px_width
self._is_hovered = False
if self._assets and img == self._assets.btn_orange:
self._style = "orange"
2026-11-03 21:21:12 +00:00
self._height = height if height else 50
2026-11-02 21:43:42 +00:00
self._default_min = 200
else:
self._style = "purple"
2026-11-03 21:21:12 +00:00
self._height = height if height else 35
2026-11-02 21:43:42 +00:00
self._default_min = 150
self._img_norm, self._img_act = self._get_scaled_images(text)
super().__init__(
parent, text=text, image=self._img_norm, compound="center", command=command,
bg=BG_COLOR, fg="white", font=self._font,
bd=0, highlightthickness=0, activebackground=BG_COLOR, activeforeground="white",
cursor="hand2"
)
self.bind("<Enter>", self._on_enter)
self.bind("<Leave>", self._on_leave)
def _get_scaled_images(self, text_str):
2026-11-03 21:21:12 +00:00
padding = 24 if self._height < 30 else 36
text_px = self._font.measure(str(text_str)) + padding
2026-11-02 21:43:42 +00:00
base_min = self._min_px_width if self._min_px_width else self._default_min
target_w = max(base_min, text_px)
if self._assets and hasattr(self._assets, "get_btn_pair"):
return self._assets.get_btn_pair(self._style, target_w, self._height)
return None, None
def _on_enter(self, event=None):
self._is_hovered = True
if self["state"] != "disabled" and self._img_act:
super().config(image=self._img_act)
def _on_leave(self, event=None):
self._is_hovered = False
if self["state"] != "disabled" and self._img_norm:
super().config(image=self._img_norm)
def config(self, **kwargs):
if "text" in kwargs:
self._img_norm, self._img_act = self._get_scaled_images(kwargs["text"])
kwargs["image"] = self._img_act if (self._is_hovered and self._img_act) else self._img_norm
super().config(**kwargs)
configure = config
2026-11-03 21:21:12 +00:00
def create_img_button(parent, text, img, img_active, command, width=None, min_px_width=None, height=None, font_size=11):
return DynamicImgButton(
parent, text, img, img_active, command,
min_px_width=min_px_width, height=height, font_size=font_size
)