tkin中vlc屏幕上的点击事件捕捉

2024-09-29 19:32:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows7和Python2.7上运行一个精简版的the python-vlc example for tkinter。在

我试图注册点击视频屏幕本身,没有任何按钮或额外的用户界面。 但是,无论我在哪里尝试.bind()作为侦听器,VLC似乎会吞噬所有输入,而且我的回调函数永远不会被调用。在

这可能是太多的代码张贴在这里,但至少它应该运行。感谢任何帮助!在

#! /usr/bin/python
# -*- coding: utf-8 -*-

import vlc, sys, os, time, platform
import Tkinter as Tk
import ttk

def onClick():
    print "a click was successful!"

class Player(Tk.Frame):
    """The main window has to deal with events.
    """
    def __init__(self, parent, title=None, url=""):
        Tk.Frame.__init__(self, parent)
        self.parent = parent
        self.url = url

        self.player = None
        self.videopanel = ttk.Frame(self.parent)
        self.canvas = Tk.Canvas(self.videopanel,bg="blue")

        #try to listen for clicks
        self.videopanel.bind('<Button-1>', onClick)
        self.canvas.bind('<Button-1>', onClick)

        self.canvas.pack(fill=Tk.BOTH,expand=1)
        self.videopanel.pack(fill=Tk.BOTH,expand=1)

        # VLC player controls
        self.Instance = vlc.Instance()
        self.player = self.Instance.media_player_new()

        self.parent.update()

        self.Media = self.Instance.media_new(self.url)
        self.player.set_media(self.Media)

        # set the window id where to render VLC's video output
        if platform.system() == 'Windows':
            self.player.set_hwnd(self.GetHandle())
        else:
            self.player.set_xwindow(self.GetHandle()) # this line messes up windows

        self.player.play()

    def GetHandle(self):
        return self.videopanel.winfo_id()

if __name__ == "__main__":

    root = Tk.Tk()

    player = Player(root, title="tkinter vlc", url="video.mp4")
    player.bind('<Button-1>', onClick)

    root.mainloop()

Tags: instanceimportselfurlbinddefframetk

热门问题