blackjack-theory-game/gui/components/hints.py
2026-06-09 20:07:08 +02:00

94 lines
3.7 KiB
Python
Executable File

import customtkinter as ctk
from PIL import Image
class HintSystem:
def __init__(self, parent):
self.parent = parent
self.hint_window = None
self.attached_windows = []
self.hint_button = None # Will be set by main GUI
def show_hint(self):
"""Original hint display functionality"""
if not hasattr(self, "hint_window") or self.hint_window is None or not self.hint_window.winfo_exists():
self._create_attached_window("hint", "Blackjack Strategy Hint", "images/hint.jpg", (550, 750))
def _create_attached_window(self, window_type, img_path, title, size):
"""Original window creation logic"""
if hasattr(self, f"{window_type}_window"):
getattr(self, f"_close_{window_type}_window")()
window = ctk.CTkToplevel(self.parent)
window.title(title)
window.geometry(f"{size[0]}x{size[1]}")
window.overrideredirect(True)
window.attributes("-topmost", True)
setattr(self, f"{window_type}_window", window)
self.attached_windows.append(window)
if window_type == "hint":
self._load_hint_image(window)
self._update_attached_windows_position()
self.parent.bind("<Configure>", lambda e: self._update_attached_windows_position())
def _update_attached_windows_position(self):
"""Original window positioning logic"""
if not hasattr(self, 'parent') or not self.parent.winfo_exists():
return
hint_window_exists = (hasattr(self, "hint_window") and
self.hint_window is not None and
self.hint_window.winfo_exists())
if hint_window_exists:
main_x = self.parent.winfo_x()
main_y = self.parent.winfo_y()
main_width = self.parent.winfo_width()
hint_x = main_x + main_width + 10
self.hint_window.geometry(f"+{hint_x}+{main_y}")
def _close_hint_window(self):
"""Safely close hint window with proper null checks"""
if hasattr(self, 'parent'):
self.parent.unbind("<Configure>")
# Close the window if it exists
if hasattr(self, "hint_window") and self.hint_window is not None:
if self.hint_window.winfo_exists():
self.hint_window.destroy()
self.hint_window = None
# Clean up references
self.attached_windows = []
# Rebind the Configure event if parent exists
if hasattr(self, 'parent') and self.parent.winfo_exists():
self.parent.bind("<Configure>", lambda e: self._update_attached_windows_position())
def _load_hint_image(self, window):
"""Original image loading with error handling"""
try:
hint_image = Image.open("images/hint.jpg")
hint_image = hint_image.resize((500, 700))
hint_ctk_image = ctk.CTkImage(hint_image, size=(500, 700))
hint_label = ctk.CTkLabel(window, image=hint_ctk_image, text="")
hint_label.pack(padx=10, pady=10)
except FileNotFoundError:
error_label = ctk.CTkLabel(window, text="Hint image not found!", font=("Arial", 20))
error_label.pack(padx=10, pady=10)
def _disable_hint_button(self):
"""Original button state control"""
if self.hint_button:
self.hint_button.configure(state="disabled")
def _enable_hint_button(self):
"""Original button state control"""
if self.hint_button:
self.hint_button.configure(state="normal")
def set_hint_button(self, button):
"""Allow main GUI to register its hint button"""
self.hint_button = button