在 tkinter 中捕获 vlc 屏幕上的点击事件 [英] Capture click event on the screen of vlc in tkinter

查看:44
本文介绍了在 tkinter 中捕获 vlc 屏幕上的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个精简版的 python-适用于 Windows 7 和 Python 2.7 上的 tkinter 的 vlc 示例.

I am running a stripped down version of the python-vlc example for tkinter on Windows 7 and Python 2.7.

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

I am trying to register clicks onto the video screen itself, without any buttons or additional UI. However, no matter where I try to .bind() a listener, VLC appears to swallow all input and my callback function is never called.

这里发布的代码可能太多了,但至少它应该可以运行.任何帮助表示赞赏!

This is probably way too much code to post here, but at least it should run. Any help appreciated!

#! /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()

推荐答案

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

Have you tried binding to the root frame?

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

您的 onClick 函数需要接受该事件作为其第一个参数:

Your onClick function will need to accept the event as its first argument:

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

这篇关于在 tkinter 中捕获 vlc 屏幕上的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆