import os import json import tkinter as tk from PIL import Image, ImageTk import vlc CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'main_data', 'app_config.txt') PLAYLIST_DIR = os.path.join(os.path.dirname(__file__), 'static_data', 'playlist') MEDIA_DATA_PATH = os.path.join(os.path.dirname(__file__), 'static_data', 'media') class SimpleTkPlayer: def __init__(self, root, playlist): self.root = root self.playlist = playlist self.current_index = 0 self.paused = False self.pause_timer = None self.label = tk.Label(root, bg='black') self.label.pack(fill=tk.BOTH, expand=True) self.create_controls() self.hide_controls() self.root.bind('', self.on_activity) self.root.bind('', self.on_activity) self.root.after(100, self.ensure_fullscreen) def ensure_fullscreen(self): self.root.attributes('-fullscreen', True) self.root.update_idletasks() def create_controls(self): self.controls_frame = tk.Frame(self.root, bg='#222', bd=2, relief='ridge') self.controls_frame.place(relx=0.98, rely=0.98, anchor='se') btn_style = { 'bg': '#333', 'fg': 'white', 'activebackground': '#555', 'activeforeground': '#00e6e6', 'font': ('Arial', 16, 'bold'), 'bd': 0, 'highlightthickness': 0, 'relief': 'flat', 'cursor': 'hand2', 'padx': 10, 'pady': 6 } self.prev_btn = tk.Button(self.controls_frame, text='⏮ Prev', command=self.prev_media, **btn_style) self.prev_btn.grid(row=0, column=0, padx=4) self.pause_btn = tk.Button(self.controls_frame, text='⏸ Pause', command=self.toggle_pause, **btn_style) self.pause_btn.grid(row=0, column=1, padx=4) self.next_btn = tk.Button(self.controls_frame, text='Next ⏭', command=self.next_media, **btn_style) self.next_btn.grid(row=0, column=2, padx=4) self.settings_btn = tk.Button(self.controls_frame, text='⚙ Settings', command=self.open_settings, **btn_style) self.settings_btn.grid(row=0, column=3, padx=4) self.exit_btn = tk.Button(self.controls_frame, text='⏻ Exit', command=self.exit_app, **btn_style) self.exit_btn.grid(row=0, column=4, padx=4) self.exit_btn.config(fg='#ff4d4d') def show_controls(self): self.controls_frame.place(relx=0.98, rely=0.98, anchor='se') self.controls_frame.lift() self.schedule_hide_controls() def hide_controls(self): self.controls_frame.place_forget() def schedule_hide_controls(self): if hasattr(self, 'hide_controls_timer') and self.hide_controls_timer: self.root.after_cancel(self.hide_controls_timer) self.hide_controls_timer = self.root.after(5000, self.hide_controls) def on_activity(self, event=None): self.show_controls() def prev_media(self): self.current_index = (self.current_index - 1) % len(self.playlist) self.show_current_media() def next_media(self): self.current_index = (self.current_index + 1) % len(self.playlist) self.show_current_media() def toggle_pause(self): if not self.paused: self.paused = True self.pause_btn.config(text='▶ Resume') self.pause_timer = self.root.after(30000, self.resume_play) else: self.resume_play() def resume_play(self): self.paused = False self.pause_btn.config(text='⏸ Pause') if self.pause_timer: self.root.after_cancel(self.pause_timer) self.pause_timer = None def play_intro_video(self): intro_path = os.path.join(os.path.dirname(__file__), 'main_data', 'intro1.mp4') if os.path.exists(intro_path): self.show_video(intro_path, on_end=self.after_intro) else: self.after_intro() def after_intro(self): self.show_current_media() self.root.after(100, self.next_media_loop) def show_video(self, file_path, on_end=None): if hasattr(self, 'vlc_player') and self.vlc_player: self.vlc_player.stop() if not hasattr(self, 'video_canvas'): self.video_canvas = tk.Canvas(self.root, bg='black', highlightthickness=0) self.video_canvas.pack(fill=tk.BOTH, expand=True) self.label.pack_forget() self.video_canvas.pack(fill=tk.BOTH, expand=True) self.root.attributes('-fullscreen', True) self.root.update_idletasks() self.vlc_instance = vlc.Instance() self.vlc_player = self.vlc_instance.media_player_new() self.vlc_player.set_mrl(file_path) self.vlc_player.set_fullscreen(True) self.vlc_player.set_xwindow(self.video_canvas.winfo_id()) self.vlc_player.play() def check_end(): if self.vlc_player.get_state() == vlc.State.Ended: self.video_canvas.pack_forget() self.label.pack(fill=tk.BOTH, expand=True) if on_end: on_end() else: self.root.after(200, check_end) check_end() def show_current_media(self): self.root.attributes('-fullscreen', True) self.root.update_idletasks() if not self.playlist: self.label.config(text="No media available", fg='white', font=('Arial', 32)) return media = self.playlist[self.current_index] file_path = os.path.join(MEDIA_DATA_PATH, media['file_name']) if file_path.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')): self.show_video(file_path, on_end=self.next_media) elif file_path.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif')): try: img = Image.open(file_path) # Fit to screen without crop or stretch screen_w = self.root.winfo_screenwidth() screen_h = self.root.winfo_screenheight() img_w, img_h = img.size scale = min(screen_w / img_w, screen_h / img_h) new_w = int(img_w * scale) new_h = int(img_h * scale) img = img.resize((new_w, new_h), Image.LANCZOS) bg = Image.new('RGB', (screen_w, screen_h), 'black') x = (screen_w - new_w) // 2 y = (screen_h - new_h) // 2 bg.paste(img, (x, y)) photo = ImageTk.PhotoImage(bg) self.label.config(image=photo, text='') self.label.image = photo except Exception as e: self.label.config(text=f"Image error: {e}", fg='red') else: self.label.config(text=f"Unsupported: {media['file_name']}", fg='yellow') def next_media_loop(self): if not self.playlist or self.paused: self.root.after(1000, self.next_media_loop) return duration = self.playlist[self.current_index].get('duration', 10) self.current_index = (self.current_index + 1) % len(self.playlist) self.show_current_media() self.root.after(duration * 1000, self.next_media_loop) def exit_app(self): # Signal the update thread to stop if stop_event is present if hasattr(self, 'stop_event') and self.stop_event: self.stop_event.set() self.root.destroy() def open_settings(self): if self.paused is not True: self.paused = True self.pause_btn.config(text='▶ Resume') settings_win = tk.Toplevel(self.root) settings_win.title('Settings') settings_win.geometry('400x300+100+100') settings_win.transient(self.root) settings_win.grab_set() tk.Label(settings_win, text='Settings', font=('Arial', 18)).pack(pady=10) # Example setting: close button tk.Button(settings_win, text='Close', command=settings_win.destroy).pack(pady=20) def on_close(): settings_win.grab_release() settings_win.destroy() self.resume_play() settings_win.protocol('WM_DELETE_WINDOW', on_close) settings_win.bind('', lambda e: self.resume_play() if not settings_win.winfo_exists() else None) def main_start(self): self.play_intro_video() def load_latest_playlist(): files = [f for f in os.listdir(PLAYLIST_DIR) if f.startswith('server_playlist_v') and f.endswith('.json')] if not files: return [] files.sort(key=lambda x: int(x.split('_v')[-1].split('.json')[0]), reverse=True) with open(os.path.join(PLAYLIST_DIR, files[0]), 'r') as f: data = json.load(f) return data.get('playlist', []) def main(): root = tk.Tk() root.title("Simple Tkinter Player") root.configure(bg='black') root.attributes('-fullscreen', True) playlist = load_latest_playlist() player = SimpleTkPlayer(root, playlist) player.main_start() root.mainloop() if __name__ == '__main__': main()