Aktualizovat spotify_widget.py
This commit is contained in:
@@ -10,7 +10,7 @@ import subprocess
|
||||
import requests
|
||||
from io import BytesIO
|
||||
import tkinter as tk
|
||||
from PIL import Image, ImageTk, ImageDraw
|
||||
from PIL import Image, ImageTk, ImageDraw, ImageChops
|
||||
import pythoncom
|
||||
|
||||
# Pokus o automatickou instalaci pycaw, pokud chybí
|
||||
@@ -48,8 +48,11 @@ SERVER_URL = "http://localhost:8080/endpoint"
|
||||
CYCLE_INTERVAL_MS = 10000 # Doba zobrazení produktu (10 sekund)
|
||||
FRAME_INTERVAL_MS = 120 # Rychlost 3D rotace fotek (120 ms)
|
||||
PARTICLE_COUNT = 60 # Počet bokeh částic
|
||||
POPUP_INTERVAL_MS = 300000 # Interval zobrazení fotka.png (5 minut = 300 000 ms)
|
||||
POPUP_DURATION_MS = 3000 # Doba zobrazení fotka.png (3 sekundy = 3 000 ms)
|
||||
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
POPUP_IMAGE_PATH = os.path.join(SCRIPT_DIR, "fotka.png")
|
||||
|
||||
KATALOG_DIR = os.path.join(SCRIPT_DIR, "katalog")
|
||||
if not os.path.exists(KATALOG_DIR):
|
||||
@@ -162,12 +165,13 @@ class FullscreenJarmarkDisplay:
|
||||
self.h = 1080
|
||||
|
||||
# Klávesové zkratky
|
||||
self.root.bind("<Escape>", lambda e: self.root.destroy())
|
||||
self.root.bind("<Escape>", lambda e: self.check_escape())
|
||||
self.root.bind("<Right>", lambda e: self.next_product())
|
||||
self.root.bind("<Left>", lambda e: self.prev_product())
|
||||
self.root.bind("<space>", lambda e: self.toggle_pause())
|
||||
self.root.bind("<r>", lambda e: self.reload_catalog_action())
|
||||
self.root.bind("<R>", lambda e: self.reload_catalog_action())
|
||||
self.root.bind("<F2>", lambda e: self.toggle_msg_input())
|
||||
|
||||
# Plátno s čistým tmavým pozadím
|
||||
self.canvas = tk.Canvas(root, bg="#07010f", highlightthickness=0)
|
||||
@@ -180,20 +184,16 @@ class FullscreenJarmarkDisplay:
|
||||
self.particle_draw_ids = []
|
||||
self.init_particles_canvas()
|
||||
|
||||
# Canvas Prvky - Název produktu se stínem pro skvělou čitelnost bez rámčku
|
||||
# Canvas Prvky - Název produktu se stínem
|
||||
self.title_shadow_id = self.canvas.create_text(0, 0, text="", fill="#000000", font=("Impact", 42, "bold"), anchor="center")
|
||||
self.title_text_id = self.canvas.create_text(0, 0, text="", fill="#ffffff", font=("Impact", 42, "bold"), anchor="center")
|
||||
|
||||
self.badge_bg_id = self.canvas.create_rectangle(0, 0, 0, 0, fill="", outline="", state="hidden")
|
||||
self.badge_text_id = self.canvas.create_text(0, 0, text="", fill="white", font=("Segoe UI", 13, "bold"), state="hidden")
|
||||
|
||||
# Produkt fotka (zcela bez rámečku, volně na pozadí)
|
||||
# Produkt fotka
|
||||
self.prod_image_id = self.canvas.create_image(0, 0, anchor="center")
|
||||
|
||||
# Celoobrazovková vrstva pro dobrovolníka (skrytá, dokud nenastane čas)
|
||||
self.volunteer_image_id = self.canvas.create_image(0, 0, anchor="nw", state="hidden")
|
||||
self.volunteer_photo = None
|
||||
|
||||
# Cenovka vpravo
|
||||
self.price_bg_id = self.canvas.create_rectangle(0, 0, 0, 0, fill="#120521", outline="#9d4edd", width=2)
|
||||
self.price_shadow_id = self.canvas.create_text(0, 0, text="", fill="#000000", font=("Segoe UI", 38, "bold"), anchor="center")
|
||||
@@ -206,7 +206,7 @@ class FullscreenJarmarkDisplay:
|
||||
# Indikátor pauzy
|
||||
self.pause_indicator_id = self.canvas.create_text(0, 0, text="", fill="#ff0055", font=("Segoe UI", 13, "bold"), anchor="w")
|
||||
|
||||
# Spotify Karta (ovládaná vaším rozšířením, vlevo dole)
|
||||
# Spotify Karta (vlevo dole)
|
||||
self.card_x1, self.card_y1 = 0, 0
|
||||
self.card_w, self.card_h = 360, 150
|
||||
self.card_bg_photo = None
|
||||
@@ -222,6 +222,30 @@ class FullscreenJarmarkDisplay:
|
||||
self.pbar_bg = self.canvas.create_line(0, 0, 0, 0, fill="#3c096c", width=5, capstyle=tk.ROUND)
|
||||
self.pbar_fg = self.canvas.create_line(0, 0, 0, 0, fill="#f72585", width=5, capstyle=tk.ROUND)
|
||||
|
||||
# Vizuální banner pro zobrazení čistého textu zprávy – odlišný font (Segoe UI bold) bez rámečku
|
||||
self.msg_banner_text = self.canvas.create_text(0, 0, text="", fill="#ffffff", font=("Segoe UI", 36, "bold"), anchor="center", state="hidden")
|
||||
self.banner_hide_job = None
|
||||
|
||||
# Stav celopodobrázkového popupu pro fotka.png (každých 5 minut na 3 sekundy)
|
||||
self.popup_win = None
|
||||
self.popup_photo = None
|
||||
self.popup_hide_job = None
|
||||
|
||||
# === SKRYTÉ VSTUPNÍ POLE PRO ZPRÁVY (Aktivuje se klávesou F2) ===
|
||||
self.msg_frame = tk.Frame(root, bg="#120521", highlightbackground="#9d4edd", highlightthickness=2)
|
||||
self.msg_entry = tk.Entry(self.msg_frame, font=("Segoe UI", 16), bg="#120521", fg="#ffffff", insertbackground="white", relief=tk.FLAT)
|
||||
self.msg_entry.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10, pady=5)
|
||||
self.msg_entry.bind("<Return>", self.handle_user_message)
|
||||
self.msg_entry.bind("<Escape>", lambda e: self.hide_msg_input())
|
||||
|
||||
# Nápověda v poli
|
||||
self.msg_entry.insert(0, "Napište zprávu a stiskněte Enter...")
|
||||
self.msg_entry.bind("<FocusIn>", self.clear_placeholder)
|
||||
self.is_msg_visible = False
|
||||
|
||||
# DŮLEŽITÉ: Okamžité skrytí prvku při inicializaci, aby nepřetékal na plochu
|
||||
self.msg_frame.place_forget()
|
||||
|
||||
# Stavové proměnné
|
||||
self.running = True
|
||||
self.is_paused = False
|
||||
@@ -238,48 +262,173 @@ class FullscreenJarmarkDisplay:
|
||||
self.current_catalog_index = 0
|
||||
self.current_frame_index = 0
|
||||
|
||||
# Pro plynulý fade efekt
|
||||
self.fade_anim_job = None
|
||||
|
||||
# Spuštění procesů
|
||||
self.load_catalog()
|
||||
self.cycle_product()
|
||||
self.animate_product_3d()
|
||||
self.animate_particles()
|
||||
self.update_clock()
|
||||
|
||||
# Spuštění časovače pro zobrazení obrázku dobrovolníka každých 15 minut
|
||||
self.schedule_volunteer_popup()
|
||||
self.schedule_popup_image()
|
||||
|
||||
threading.Thread(target=self.poll_data, daemon=True).start()
|
||||
|
||||
def schedule_volunteer_popup(self):
|
||||
# 15 minut = 15 * 60 * 1000 milisekund = 900 000 ms
|
||||
self.root.after(900000, self.show_volunteer_image)
|
||||
def schedule_popup_image(self):
|
||||
""" Naplánuje zobrazení fotka.png každých 5 minut """
|
||||
self.root.after(POPUP_INTERVAL_MS, self.trigger_popup_image)
|
||||
|
||||
def trigger_popup_image(self):
|
||||
""" Zobrazí fotka.png na celou obrazovku na 3 sekundy a naplánuje další cyklus """
|
||||
self.show_fullscreen_popup()
|
||||
self.schedule_popup_image()
|
||||
|
||||
def _build_popup_photo(self, target_w, target_h):
|
||||
""" Načte a přizpůsobí fotka.png na zadané rozměry """
|
||||
if not os.path.exists(POPUP_IMAGE_PATH):
|
||||
print(f"⚠️ Obrázek '{POPUP_IMAGE_PATH}' nebyl nalezen.")
|
||||
return None
|
||||
|
||||
def show_volunteer_image(self):
|
||||
volunteer_path = os.path.join(SCRIPT_DIR, "dobrovolnik.png")
|
||||
if os.path.exists(volunteer_path):
|
||||
try:
|
||||
img = Image.open(volunteer_path).convert("RGBA")
|
||||
# Přizpůsobení velikosti na aktuální rozlišení obrazovky
|
||||
img = img.resize((self.w, self.h), Image.Resampling.LANCZOS)
|
||||
self.volunteer_photo = ImageTk.PhotoImage(img)
|
||||
raw_img = Image.open(POPUP_IMAGE_PATH).convert("RGB")
|
||||
img_w, img_h = raw_img.size
|
||||
if img_w <= 0 or img_h <= 0:
|
||||
return None
|
||||
|
||||
# Zobrazení obrázku na celou obrazovku nad ostatní prvky
|
||||
self.canvas.itemconfig(self.volunteer_image_id, image=self.volunteer_photo, state="normal")
|
||||
self.canvas.tag_raise(self.volunteer_image_id)
|
||||
scale = min(target_w / img_w, target_h / img_h)
|
||||
new_w = max(1, int(img_w * scale))
|
||||
new_h = max(1, int(img_h * scale))
|
||||
|
||||
# Naplánování skrytí po 3 sekundách (3000 ms)
|
||||
self.root.after(3000, self.hide_volunteer_image)
|
||||
resized = raw_img.resize((new_w, new_h), Image.Resampling.LANCZOS)
|
||||
return ImageTk.PhotoImage(resized)
|
||||
except Exception as e:
|
||||
print(f"Chyba při načítání obrázku dobrovolníka: {e}")
|
||||
self.schedule_volunteer_popup()
|
||||
else:
|
||||
print(f"⚠️ Soubor 'dobrovolnik.png' nebyl nalezen v adresáři: {SCRIPT_DIR}")
|
||||
self.schedule_volunteer_popup()
|
||||
print(f"Chyba při načítání {POPUP_IMAGE_PATH}: {e}")
|
||||
return None
|
||||
|
||||
def hide_volunteer_image(self):
|
||||
# Skrytí obrázku a naplánování dalšího spuštění za 15 minut
|
||||
self.canvas.itemconfig(self.volunteer_image_id, state="hidden")
|
||||
self.schedule_volunteer_popup()
|
||||
def show_fullscreen_popup(self):
|
||||
""" Zobrazí fotka.png přes celou obrazovku na POPUP_DURATION_MS (3 s) """
|
||||
self.hide_fullscreen_popup()
|
||||
|
||||
screen_w = self.root.winfo_screenwidth()
|
||||
screen_h = self.root.winfo_screenheight()
|
||||
photo = self._build_popup_photo(screen_w, screen_h)
|
||||
if not photo:
|
||||
return
|
||||
|
||||
win = tk.Toplevel(self.root)
|
||||
win.overrideredirect(True)
|
||||
win.geometry(f"{screen_w}x{screen_h}+0+0")
|
||||
win.configure(bg="#07010f")
|
||||
win.attributes("-topmost", True)
|
||||
|
||||
lbl = tk.Label(win, image=photo, bg="#07010f", borderwidth=0)
|
||||
lbl.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
self.popup_win = win
|
||||
self.popup_photo = photo
|
||||
self.popup_hide_job = self.root.after(POPUP_DURATION_MS, self.hide_fullscreen_popup)
|
||||
|
||||
def hide_fullscreen_popup(self):
|
||||
""" Zavře a odstraní celopodobrázkový popup s fotka.png """
|
||||
if self.popup_hide_job:
|
||||
try:
|
||||
self.root.after_cancel(self.popup_hide_job)
|
||||
except Exception:
|
||||
pass
|
||||
self.popup_hide_job = None
|
||||
|
||||
if self.popup_win is not None:
|
||||
try:
|
||||
self.popup_win.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
self.popup_win = None
|
||||
|
||||
self.popup_photo = None
|
||||
|
||||
def toggle_msg_input(self):
|
||||
""" Zobrazí nebo schová skryté zprávové pole po stisknutí F2 """
|
||||
if self.is_msg_visible:
|
||||
self.hide_msg_input()
|
||||
else:
|
||||
self.show_msg_input()
|
||||
|
||||
def show_msg_input(self):
|
||||
self.is_msg_visible = True
|
||||
box_w, box_h = 500, 50
|
||||
bx = (self.w - box_w) // 2
|
||||
by = self.h - 120
|
||||
self.msg_frame.place(x=bx, y=by, width=box_w, height=box_h)
|
||||
|
||||
current_text = self.msg_entry.get()
|
||||
if not current_text or current_text == "✓ Zpráva uložena!":
|
||||
self.msg_entry.delete(0, tk.END)
|
||||
self.msg_entry.insert(0, "Napište zprávu a stiskněte Enter...")
|
||||
|
||||
self.msg_entry.focus_set()
|
||||
self.msg_entry.selection_range(0, tk.END)
|
||||
|
||||
def hide_msg_input(self):
|
||||
self.is_msg_visible = False
|
||||
self.msg_frame.place_forget()
|
||||
self.canvas.focus_set()
|
||||
|
||||
def clear_placeholder(self, event):
|
||||
current_text = self.msg_entry.get()
|
||||
if current_text in ["Napište zprávu a stiskněte Enter...", "✓ Zpráva uložena!"]:
|
||||
self.msg_entry.delete(0, tk.END)
|
||||
|
||||
def handle_user_message(self, event):
|
||||
msg = self.msg_entry.get().strip()
|
||||
# Pokud je zpráva prázdná, odstraníme starou zprávu z obrazovky
|
||||
if not msg or msg == "Napište zprávu a stiskněte Enter..." or msg == "✓ Zpráva uložena!":
|
||||
self.hide_onscreen_notification()
|
||||
self.hide_msg_input()
|
||||
else:
|
||||
print(f"💬 [UŽIVATELSKÁ ZPRÁVA]: {msg}")
|
||||
with open(os.path.join(SCRIPT_DIR, "user_messages.txt"), "a", encoding="utf-8") as f:
|
||||
f.write(f"[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}\n")
|
||||
|
||||
# Zobrazení čistého textu s odlišným fontem přímo na obrazovce
|
||||
self.show_onscreen_notification(msg)
|
||||
|
||||
self.msg_entry.delete(0, tk.END)
|
||||
self.msg_entry.insert(0, "✓ Zpráva uložena!")
|
||||
self.root.after(1000, self.hide_msg_input)
|
||||
|
||||
def show_onscreen_notification(self, message):
|
||||
""" Zobrazí čistý velký text zprávy s jiným fontem (Segoe UI) zcela bez rámečku """
|
||||
if self.banner_hide_job:
|
||||
self.root.after_cancel(self.banner_hide_job)
|
||||
self.banner_hide_job = None
|
||||
|
||||
display_text = message
|
||||
self.canvas.itemconfig(self.msg_banner_text, text=display_text)
|
||||
|
||||
# Změna fontu na Segoe UI (odlišné od produktu)
|
||||
font_msg_sz = max(28, int(self.h * 0.050))
|
||||
self.canvas.itemconfig(self.msg_banner_text, font=("Segoe UI", font_msg_sz, "bold"), fill="#ffffff")
|
||||
|
||||
bx = self.w // 2
|
||||
by = self.h - 180
|
||||
|
||||
self.canvas.coords(self.msg_banner_text, bx, by)
|
||||
self.canvas.itemconfig(self.msg_banner_text, state="normal")
|
||||
self.canvas.tag_raise(self.msg_banner_text)
|
||||
|
||||
def hide_onscreen_notification(self):
|
||||
self.canvas.itemconfig(self.msg_banner_text, state="hidden")
|
||||
|
||||
def check_escape(self):
|
||||
if self.popup_win is not None:
|
||||
self.hide_fullscreen_popup()
|
||||
elif self.is_msg_visible:
|
||||
self.hide_msg_input()
|
||||
else:
|
||||
self.running = False
|
||||
self.hide_fullscreen_popup()
|
||||
self.root.destroy()
|
||||
|
||||
def trigger_ad_sound(self):
|
||||
print("🔇 Ztlumuji vše kromě Pythonu...")
|
||||
@@ -351,7 +500,7 @@ class FullscreenJarmarkDisplay:
|
||||
|
||||
# 2. Produkt
|
||||
self.canvas.coords(self.prod_image_id, w * 0.5, h * 0.48)
|
||||
self.update_product_frame()
|
||||
self.update_product_frame(smooth_fade=False)
|
||||
|
||||
# 3. Cenovka vpravo
|
||||
font_price_sz = max(22, int(h * 0.050))
|
||||
@@ -463,19 +612,19 @@ class FullscreenJarmarkDisplay:
|
||||
|
||||
def next_product(self):
|
||||
if self.catalog_items:
|
||||
self.show_product_by_index(self.current_catalog_index)
|
||||
self.show_product_by_index(self.current_catalog_index, smooth=True)
|
||||
self.current_catalog_index = (self.current_catalog_index + 1) % len(self.catalog_items)
|
||||
|
||||
def prev_product(self):
|
||||
if self.catalog_items:
|
||||
prev_idx = (self.current_catalog_index - 2) % len(self.catalog_items)
|
||||
self.show_product_by_index(prev_idx)
|
||||
self.show_product_by_index(prev_idx, smooth=True)
|
||||
self.current_catalog_index = (prev_idx + 1) % len(self.catalog_items)
|
||||
|
||||
def reload_catalog_action(self):
|
||||
self.load_catalog()
|
||||
if self.catalog_items:
|
||||
self.show_product_by_index(0)
|
||||
self.show_product_by_index(0, smooth=True)
|
||||
self.current_catalog_index = 1 % len(self.catalog_items)
|
||||
|
||||
def update_card_background(self, rgba_color):
|
||||
@@ -543,12 +692,12 @@ class FullscreenJarmarkDisplay:
|
||||
self.load_catalog()
|
||||
|
||||
if self.catalog_items:
|
||||
self.show_product_by_index(self.current_catalog_index)
|
||||
self.show_product_by_index(self.current_catalog_index, smooth=True)
|
||||
self.current_catalog_index = (self.current_catalog_index + 1) % len(self.catalog_items)
|
||||
|
||||
self.root.after(CYCLE_INTERVAL_MS, self.cycle_product)
|
||||
|
||||
def show_product_by_index(self, index):
|
||||
def show_product_by_index(self, index, smooth=False):
|
||||
if not self.catalog_items:
|
||||
return
|
||||
|
||||
@@ -597,18 +746,18 @@ class FullscreenJarmarkDisplay:
|
||||
self.canvas.itemconfig(self.badge_bg_id, state="hidden")
|
||||
|
||||
self.current_frame_index = 0
|
||||
self.update_product_frame()
|
||||
self.update_product_frame(smooth_fade=smooth)
|
||||
|
||||
def animate_product_3d(self):
|
||||
if self.active_item:
|
||||
paths = self.active_item["image_paths"]
|
||||
if len(paths) > 1:
|
||||
self.current_frame_index = (self.current_frame_index + 1) % len(paths)
|
||||
self.update_product_frame()
|
||||
self.update_product_frame(smooth_fade=False)
|
||||
|
||||
self.root.after(FRAME_INTERVAL_MS, self.animate_product_3d)
|
||||
|
||||
def update_product_frame(self):
|
||||
def update_product_frame(self, smooth_fade=False):
|
||||
if not self.active_item:
|
||||
return
|
||||
|
||||
@@ -621,7 +770,15 @@ class FullscreenJarmarkDisplay:
|
||||
max_h = int(self.h * 0.52)
|
||||
|
||||
raw_img.thumbnail((max_w, max_h), Image.Resampling.LANCZOS)
|
||||
self.current_prod_photo = ImageTk.PhotoImage(raw_img)
|
||||
|
||||
if smooth_fade:
|
||||
self.run_fade_animation(raw_img)
|
||||
else:
|
||||
target_padded = Image.new("RGBA", (max_w, max_h), (0, 0, 0, 0))
|
||||
tw, th = raw_img.size
|
||||
target_padded.paste(raw_img, ((max_w - tw) // 2, (max_h - th) // 2), raw_img)
|
||||
|
||||
self.current_prod_photo = ImageTk.PhotoImage(target_padded)
|
||||
self.canvas.itemconfig(self.prod_image_id, image=self.current_prod_photo)
|
||||
except Exception as e:
|
||||
print(f"Chyba při načítání snímku {img_path}: {e}")
|
||||
@@ -629,6 +786,34 @@ class FullscreenJarmarkDisplay:
|
||||
else:
|
||||
self.canvas.itemconfig(self.prod_image_id, image="")
|
||||
|
||||
def run_fade_animation(self, target_img):
|
||||
""" Provede plynulý přechod (fade) na novou fotografii produktu """
|
||||
if self.fade_anim_job:
|
||||
self.root.after_cancel(self.fade_anim_job)
|
||||
|
||||
max_w = int(self.w * 0.42)
|
||||
max_h = int(self.h * 0.52)
|
||||
|
||||
target_padded = Image.new("RGBA", (max_w, max_h), (0, 0, 0, 0))
|
||||
tw, th = target_img.size
|
||||
target_padded.paste(target_img, ((max_w - tw) // 2, (max_h - th) // 2), target_img)
|
||||
|
||||
base_bg = Image.new("RGBA", (max_w, max_h), (7, 1, 15, 0))
|
||||
|
||||
steps = 5
|
||||
delay = 25
|
||||
|
||||
def step_fade(current_step):
|
||||
faded_img = Image.blend(base_bg, target_padded, current_step / steps)
|
||||
|
||||
self.current_prod_photo = ImageTk.PhotoImage(faded_img)
|
||||
self.canvas.itemconfig(self.prod_image_id, image=self.current_prod_photo)
|
||||
|
||||
if current_step < steps:
|
||||
self.fade_anim_job = self.root.after(delay, lambda: step_fade(current_step + 1))
|
||||
|
||||
step_fade(1)
|
||||
|
||||
def poll_data(self):
|
||||
pythoncom.CoInitialize()
|
||||
while self.running:
|
||||
|
||||
Reference in New Issue
Block a user