禁用在libvlc Python中播放视频时打开新窗口



我正在开发视频播放器,我想知道如何在新窗口中禁用打开和播放所选视频?有没有办法在原始窗口中打开它?我完全不知道该怎么做。谢谢大家。

左边:原始窗口;右侧:附加窗口

import os, sys, pathlib, vlc
import tkinter as tk
from tkinter import *
from tkinter import filedialog
class BaseTkContainer:
def __init__(self):
self.tk_instance = tk.Tk()
self.tk_instance.title("py player")
self.tk_instance.geometry("640x510")
self.tk_instance.configure(background='black')
# Create Control and Display panel
controlFrame = LabelFrame(self.tk_instance,relief=GROOVE, bg='#383859')
controlFrame.place(x=0, y=480, width=640, height=30)
playBtn = Button(controlFrame, text="Play", command=self.PlayMovie, bd=0).grid(row=0, column=0, sticky=W)
stopBtn = Button(controlFrame, text="Stop", command=self.StopMovie, bd=0).grid(row=0, column=1, sticky=W)
pauseBtn = Button(controlFrame, text="Pause", command=self.PauseMovie, bd=0).grid(row=0, column=2, sticky=W)
openBtn = Button(controlFrame, text="Open", command=self.OpenFile, bd=0).grid(row=0, column=3, sticky=W)
testBtn = Button(controlFrame, text="Time", command=self.InfoDisplay, bd=0).grid(row=0, column=5, sticky=W)
self.timeLabel = Label(controlFrame, font=("calibri",11,"bold"))
self.nameLabel = Label(controlFrame, font=("calibri",11,"bold"))
self.volumeScale = Scale(controlFrame, from_=0, to=100, orient=HORIZONTAL, command=self.MovieVolume, sliderlength=15, bd=0, showvalue=0, length=200)
self.volumeScale.grid(row=0, column=4, sticky=W)
self.volumeScale.set(50)
def MovieVolume(self, setValue):
"""Volume settings"""
volume = self.volumeScale.get()
try:
self.mediaPlayer.audio_set_volume(volume)
except AttributeError:
pass
def PlayMovie(self):
"""Play a file"""
self.mediaPlayer.play()
self.mediaPlayerState = "State.Playing"
self.InfoDisplay()
def StopMovie(self):
"""Stop the player"""
self.mediaPlayer.stop()
def PauseMovie(self):
"""Pause the player"""
self.mediaPlayer.pause()
def OpenFile(self):
"""Open window explorer to select a movie to play"""
file = filedialog.askopenfilename()
directoryName = os.path.dirname(file)
self.fileName = os.path.basename(file)
media = str(os.path.join(directoryName, self.fileName))
self.mediaPlayer = vlc.MediaPlayer(media)
def InfoDisplay(self):
"""Display basic info about a movie"""
--snip--
root = BaseTkContainer()
root.tk_instance.mainloop()

如果你没有告诉libvlc要绘制到哪个窗口,它将自己创建一个并绘制它。

因此,如果您希望libvlc在应用程序的指定窗口中绘制,则需要根据您的主机平台告诉它:

  • libvlc_media_player_set_hwnd在视窗上,
  • libvlc_media_player_set_xwindowLinux 上,
  • libvlc_media_player_set_nsobject苹果平台上。

有关所需的确切文档和参数,请参阅官方文档。由于您使用的是 Python,因此您应该在绑定中搜索这些 C 函数是如何从 Python 绑定中公开的。

相关内容

最新更新