在tkinter中的vlc屏幕上捕获单击事件

时间:2018-04-17 03:55:30

标签: python tkinter vlc

我在Windows 7和Python 2.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()

1 个答案:

答案 0 :(得分:0)

您是否尝试过绑定到根框架?

root.bind('<Button-1>', onClick)

你的onClick函数需要接受事件作为它的第一个参数:

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