问题
I am running a stripped down version of the python-vlc example for tkinter on Windows 7 and Python 2.7.
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()
回答1:
Have you tried binding to the root frame?
root.bind('<Button-1>', onClick)
Your onClick function will need to accept the event as its first argument:
def onClick(e):
print "a click was successful!"
来源:https://stackoverflow.com/questions/49869659/capture-click-event-on-the-screen-of-vlc-in-tkinter