You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派Tkinter队列管理应用中如何嵌入视频?

Hey Akash,

I’ve tackled exactly this problem on a Raspberry Pi for a queue management system before—embedding video in a Tkinter window is totally doable, and I’ll share two reliable methods that work well on Pi hardware. Let’s start with the most robust option first:

VLC has great hardware acceleration support on Raspberry Pi, so it’s perfect for smooth video playback without eating too much CPU. Here’s how to set it up:

Step 1: Install Dependencies

First, install the system-level VLC package and the Python bindings:

sudo apt-get update && sudo apt-get install vlc -y
pip install python-vlc

Step 2: Full Tkinter App Example

This code creates a split window—left for queue info, right for video playback:

import tkinter as tk
import vlc

class QueueManager(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Raspberry Pi Queue System")
        self.geometry("1280x720")  # Match your Pi's screen resolution

        # Left Panel: Queue Display
        self.queue_panel = tk.Frame(self, width=400, bg="#f5f5f5")
        self.queue_panel.pack(side=tk.LEFT, fill=tk.BOTH, padx=10, pady=10)

        # Example queue elements (you can expand these later)
        tk.Label(self.queue_panel, text="Current Ticket", font=("Arial", 22)).pack(pady=15)
        self.ticket_num = tk.Label(self.queue_panel, text="A007", font=("Arial", 40, "bold"))
        self.ticket_num.pack(pady=5)
        self.counter_num = tk.Label(self.queue_panel, text="Counter 2", font=("Arial", 30))
        self.counter_num.pack(pady=5)

        # Right Panel: Video Player
        self.video_panel = tk.Frame(self, bg="black")
        self.video_panel.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)

        # Initialize VLC Player
        # Enable hardware acceleration for Pi (critical for smooth playback)
        self.vlc_instance = vlc.Instance("--hwaccel auto --avcodec-hw=any")
        self.player = self.vlc_instance.media_player_new()

        # Bind the player to the Tkinter frame (uses the frame's window ID)
        self.player.set_hwnd(self.video_panel.winfo_id())

        # Load your video file (replace with your path)
        video_path = "/home/pi/queue_promo.mp4"
        media = self.vlc_instance.media_new(video_path)
        self.player.set_media(media)

        # Start playback
        self.player.play()

        # Cleanup on window close
        self.protocol("WM_DELETE_WINDOW", self.cleanup)

    def cleanup(self):
        self.player.stop()
        self.destroy()

if __name__ == "__main__":
    app = QueueManager()
    app.mainloop()

Key Notes for This Method:

  • The --hwaccel auto flag tells VLC to use Pi's hardware decoder, which prevents lag even on older Pi models.
  • set_hwnd() links the VLC player directly to your Tkinter frame—no separate video window pops up.
  • Test with a video encoded in H.264 (most common format supported by Pi's hardware).

Method 2: Use Pygame (Alternative)

If you’re already familiar with Pygame, this works too, though it has slightly worse format compatibility. Here’s a quick example:

Step 1: Install Pygame

pip install pygame

Step 2: Embedded Video Code

import tkinter as tk
import pygame
import os

class QueueManager(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Raspberry Pi Queue System")
        self.geometry("1280x720")

        # Left Queue Panel (same as before)
        self.queue_panel = tk.Frame(self, width=400, bg="#f5f5f5")
        self.queue_panel.pack(side=tk.LEFT, fill=tk.BOTH, padx=10, pady=10)
        tk.Label(self.queue_panel, text="Current Ticket", font=("Arial", 22)).pack(pady=15)
        self.ticket_num = tk.Label(self.queue_panel, text="A007", font=("Arial", 40, "bold"))
        self.ticket_num.pack(pady=5)
        self.counter_num = tk.Label(self.queue_panel, text="Counter 2", font=("Arial", 30))
        self.counter_num.pack(pady=5)

        # Right Video Panel
        self.video_panel = tk.Frame(self, bg="black")
        self.video_panel.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)

        # Initialize Pygame and bind to Tkinter frame
        os.environ['SDL_WINDOWID'] = str(self.video_panel.winfo_id())
        os.environ['SDL_VIDEODRIVER'] = 'x11'  # Required for Pi
        pygame.init()
        self.screen = pygame.display.set_mode((0,0), pygame.NOFRAME)

        # Load and play video
        self.video = pygame.movie.Movie("/home/pi/queue_promo.mp4")
        self.video.set_display(self.screen)
        self.video.play()

        # Update Pygame frame regularly to avoid freezing
        self.update_video()

        self.protocol("WM_DELETE_WINDOW", self.cleanup)

    def update_video(self):
        if self.video.get_busy():
            pygame.display.update()
        self.after(10, self.update_video)

    def cleanup(self):
        self.video.stop()
        pygame.quit()
        self.destroy()

if __name__ == "__main__":
    app = QueueManager()
    app.mainloop()

Caveats for Pygame:

  • Pygame’s movie module only supports certain codecs (like MPEG-4 Part 2). If your video won’t play, convert it to a compatible format first.
  • You need the update_video() loop to refresh the frame—without it, the video will freeze.

Final Tips for Raspberry Pi:

  • If you’re using a Pi 4 or newer, enable hardware acceleration in raspi-config (under Advanced Options > GL Driver) for even smoother playback.
  • Test your video file first with vlc /path/to/video.mp4 in the terminal to make sure it plays natively on your Pi.

Hope this gets you up and running—let me know if you hit any snags!

内容的提问来源于stack exchange,提问作者Akash Nil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:19:45