YouTube 视频嵌入 pyqt

2024-02-10

如何使用 PyQt5 嵌入 youtube 视频?我尝试执行以下操作,但它给了我一个未解决的错误:

DirectShowService:doRender 未解决的错误代码

from PyQt5 import QtWidgets,QtCore,QtGui
import sys, time
from PyQt5.QtCore import Qt,QUrl
from PyQt5 import QtWebKit
from PyQt5 import QtWebKitWidgets
from PyQt5.QtWebKit import QWebSettings
#from PyQt5 import QtWebEngineWidgets #import QWebEngineView,QWebEngineSettings

class window(QtWidgets.QMainWindow):
    def __init__(self):
        QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled,True)
        super(window,self).__init__()
        self.centralwid=QtWidgets.QWidget(self)
        self.vlayout=QtWidgets.QVBoxLayout()
        self.webview=QtWebKitWidgets.QWebView()
        self.webview.setUrl(QUrl("https://www.youtube.com/watch?v=Mq4AbdNsFVw"))
        self.vlayout.addWidget(self.webview)
        self.centralwid.setLayout(self.vlayout)
        self.setCentralWidget(self.centralwid)
        self.show()

app=QtWidgets.QApplication([])
ex=window()
sys.exit(app.exec_())

您正在从 PyQt5 导入一些已弃用的模块(QtWebKit http://pyqt.sourceforge.net/Docs/PyQt5/QtWebKit.html, and QtWebKitWidgets http://pyqt.sourceforge.net/Docs/PyQt5/QtWebKitWidgets.html#PyQt5-QtWebKitWidgets)。看来您在导入的底部注释了正确的路径。

如果您解决这些问题并使用正确的模块(QtWebEngineCore http://pyqt.sourceforge.net/Docs/PyQt5/QtWebEngineCore.html?highlight=webenginecore##PyQt5-QtWebEngineCore, QtWebEngineWidgets http://pyqt.sourceforge.net/Docs/PyQt5/QtWebEngineWidgets.html?highlight=qtwebenginewidgets##PyQt5-QtWebEngineWidgets)它适用于我的系统。

from PyQt5 import QtWidgets,QtCore,QtGui
import sys, time
from PyQt5.QtCore import Qt,QUrl
from PyQt5 import QtWebEngineWidgets
from PyQt5 import QtWebEngineCore
from PyQt5.QtWebEngineWidgets import QWebEngineSettings

class window(QtWidgets.QMainWindow):
    def __init__(self):
        QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.PluginsEnabled,True)
        super(window,self).__init__()
        self.centralwid=QtWidgets.QWidget(self)
        self.vlayout=QtWidgets.QVBoxLayout()
        self.webview=QtWebEngineWidgets.QWebEngineView()
        self.webview.setUrl(QUrl("https://www.youtube.com/watch?v=Mq4AbdNsFVw"))
        self.vlayout.addWidget(self.webview)
        self.centralwid.setLayout(self.vlayout)
        self.setCentralWidget(self.centralwid)
        self.show()

app=QtWidgets.QApplication([])
ex=window()
sys.exit(app.exec_())

我得到的输出如下所示(这似乎是正确的):

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

YouTube 视频嵌入 pyqt 的相关文章

随机推荐