如何使用 PyQt5 为 youtube 应用程序创建全屏模式? [英] How to create full screen mode for a youtube application with PyQt5?

查看:52
本文介绍了如何使用 PyQt5 为 youtube 应用程序创建全屏模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个应用程序,既可以在 youtube 上观看视频,也可以在 Internet 上进行搜索.

We have created an application to watch videos on youtube but also with the possibility to search on the Internet.

问题是在全屏方面我无法让它以我想要的方式全屏工作,但是当我想退出全屏时,我得到两个窗口而不是一个窗口,或者如果我被卡住了一个窗口,我删除了QWebEngineView.我对这个 Qt 框架没有太多经验,如果你能帮助我,我将不胜感激.

The problem is that on the full screen side I can't make it work the way I want it to full screen, but when I want to exit full screen, I get two windows instead of one or if I get stuck with a window, I delete QWebEngineView. I don't have much experience with this Qt framework and I will be grateful if you help me.

由于我使用的是 PyQt5 版本 5.13.2

Since I use PyQt5 version 5.13.2

代码就是这样

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1200, 800)

        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")

        self.hboxlayout = QtWidgets.QHBoxLayout(self.centralWidget)
        self.hboxlayout.setSpacing(6)
        self.hboxlayout.setContentsMargins(0, 0, 0, 0)
        self.hboxlayout.setObjectName("hboxlayout")

        #bara de navigare pe care sun asezate butoanele beak,inaninte etc.
        MainWindow.setCentralWidget(self.centralWidget)
        self.tbNavigate = QtWidgets.QToolBar(MainWindow)
        self.tbNavigate.setOrientation(QtCore.Qt.Horizontal)
        self.tbNavigate.setObjectName("tbNavigate")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.tbNavigate)


        self.browser=QWebEngineView(MainWindow)
        self.browser.load(QUrl('https://youtube.com'))
        MainWindow.setCentralWidget(self.browser)
        QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True)
        self.browser.page().fullScreenRequested.connect(self.FullscreenRequest)

        #butonul back
        self.actionBack = QtWidgets.QAction("Back")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("icone/back.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionBack.setIcon(icon1)
        self.actionBack.setObjectName("actionBack")

        #butonul inaite
        self.actionForward = QtWidgets.QAction("Forward")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("icone/next.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionForward.setIcon(icon2)
        self.actionForward.setObjectName("actionForward")



        #butonul refresh
        self.actionRefresh = QtWidgets.QAction("Reload this page")
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap("icone/baseline_refresh_black_18dp.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionRefresh.setIcon(icon4)
        self.actionRefresh.setObjectName("actionRefresh")

        #Butonul home
        self.actionHome = QtWidgets.QAction("Home")
        icon5 = QtGui.QIcon()
        icon5.addPixmap(QtGui.QPixmap("icone/home (1).png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionHome.setIcon(icon5)
        self.actionHome.setObjectName("actionHome")

        self.actionFileClose = QtWidgets.QAction(MainWindow)
        self.actionFileClose.setObjectName("actionFileClose")

        #Sll icon or no-sll
        self.httpsicon = QLabel()
        icon6 = QtGui.QIcon()
        icon6.addPixmap(QtGui.QPixmap("icone/lock.png"), QtGui.QIcon.Normal,QtGui.QIcon.Off)
        #self.httpsicon.setIcon(icon6)
        self.httpsicon.setObjectName("httpsicon")






        #functinare butonelor pe bara de navigare
        self.tbNavigate.addAction(self.actionBack)
        self.tbNavigate.addAction(self.actionForward)
        self.tbNavigate.addAction(self.actionRefresh)
        self.tbNavigate.addAction(self.actionHome)
        self.tbNavigate.addWidget(self.httpsicon)

        #self.tbNavigate.addSeparator()



        self.actionBack.triggered.connect( self.browser.back)
        self.actionForward.triggered.connect(self.browser.forward)
        self.actionRefresh.triggered.connect(self.browser.reload)
        self.actionHome.triggered.connect(self.navigateHome)

        #self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)



        self.addressbar =QLineEdit()
        self.addressbar.returnPressed.connect(self.navigate_to_url)
        self.tbNavigate.addWidget(self.addressbar)
        #citeste url
        self.browser.urlChanged.connect(self.update_urlBar)


    def navigateHome(self):

        self.browser.setUrl(QUrl("https://www.youtube.com"))

    def navigate_to_url(self):  
        q = QUrl(self.addressbar.text())
        if q.scheme() == "":
            q.setScheme("https")

        self.browser.setUrl(q)

    def update_urlBar(self, q):

        if q.scheme() == 'https':
            self.httpsicon.setPixmap(QPixmap("icone/ssl.png"))

        else:
            self.httpsicon.setPixmap(QPixmap("icone/lock.png"))

        self.addressbar.setText(q.toString())
        self.addressbar.setCursorPosition(0)

    def FullscreenRequest(self, request):

       print("requested")

       if(request.toggleOn()):
          request.accept()

          self.browser.setParent(None)

          self.browser.showFullScreen()

       else:
          request.accept()
          self.browser.showNormal()

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
#sys.exit(app.exec_())
app.exec_()

问题出在这里:

def FullscreenRequest(self, request):

       print("requested")

       if(request.toggleOn()):
          request.accept()

          self.browser.setParent(None)

          self.browser.showFullScreen()

       else:
          request.accept()
          self.browser.showNormal()

推荐答案

解决方案是将 QWebEngineView 重置为 centralwidget.

The solution is to reset the QWebEngineView as centralwidget.

如果你打算使用Qt Designer,那么你不应该像警告中指出的那样修改pyuic生成的类,原因之一是这个类不是一个小部件,所以很多时候你会遇到问题,但是在您的情况下,您实际上已经删除了 Qt Designer 提供的内容,因此我重新编写了您的应用程序.

If you are going to use Qt Designer then you should not modify the class generated by pyuic as indicated in the warning, and one of the reasons is that this class is not a widget, so many times you will have problems, but in your case you have practically deleted what Qt Designer has provided so I rewrote your application.

解决这个问题,通过设置 setParent(None) 你表明小部件不是窗口的一部分,因此将成为另一个新窗口的一部分,该窗口小部件是顶层,所以如果你想恢复它,你必须将它设置为 centralWidget 以便再次成为窗口的一部分.

Going to the problem, by setting setParent(None) you are indicating that the widget is not part of the window and therefore will be part of another new window where that widget is the toplevel, so if you want to restore it you must set it as centralWidget so that Be part of the window again.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.browser = QtWebEngineWidgets.QWebEngineView()
        self.setCentralWidget(self.browser)

        self.tbNavigate = QtWidgets.QToolBar(orientation=QtCore.Qt.Horizontal)
        self.addToolBar(QtCore.Qt.TopToolBarArea, self.tbNavigate)

        action_data = [
            ("Back", "icone/next.png", self.browser.back),
            ("Forward", "icone/next.png", self.browser.forward),
            (
                "Reload this page",
                "icone/baseline_refresh_black_18dp.png",
                self.browser.reload,
            ),
            ("Home", "icone/home (1).png", self.navigateHome),
        ]

        for text, icon_path, slot in action_data:
            icon = QtGui.QIcon()
            icon.addPixmap(
                QtGui.QPixmap(icon_path), QtGui.QIcon.Normal, QtGui.QIcon.Off
            )
            action = QtWidgets.QAction(text, icon=icon, triggered=slot, parent=self)
            self.tbNavigate.addAction(action)

        self.httpsicon = QtWidgets.QLabel()
        self.tbNavigate.addWidget(self.httpsicon)

        self.addressbar = QtWidgets.QLineEdit()
        self.addressbar.returnPressed.connect(self.navigate_to_url)
        self.tbNavigate.addWidget(self.addressbar)
        self.browser.urlChanged.connect(self.update_urlBar)

        self.browser.load(QtCore.QUrl("https://youtube.com"))
        global_settings = QtWebEngineWidgets.QWebEngineSettings.globalSettings()

        for attr in (
            QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled,
            QtWebEngineWidgets.QWebEngineSettings.FullScreenSupportEnabled,
        ):
            global_settings.setAttribute(attr, True)
        self.browser.page().fullScreenRequested.connect(self.FullscreenRequest)

    @QtCore.pyqtSlot()
    def navigateHome(self):
        self.browser.setUrl(QtCore.QUrl("https://www.youtube.com"))

    @QtCore.pyqtSlot()
    def navigate_to_url(self):
        q = QtCore.QUrl(self.addressbar.text())
        if not q.scheme():
            q.setScheme("https")
        self.browser.setUrl(q)

    @QtCore.pyqtSlot(QtCore.QUrl)
    def update_urlBar(self, url):
        pixmap = (
            QtGui.QPixmap("icone/ssl.png")
            if url.scheme() == "https"
            else QtGui.QPixmap("icone/lock.png")
        )
        self.httpsicon.setPixmap(pixmap)
        self.addressbar.setText(url.toString())
        self.addressbar.setCursorPosition(0)

    @QtCore.pyqtSlot("QWebEngineFullScreenRequest")
    def FullscreenRequest(self, request):
        request.accept()
        if request.toggleOn():
            self.browser.setParent(None)
            self.browser.showFullScreen()
        else:
            self.setCentralWidget(self.browser)
            self.browser.showNormal()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

这篇关于如何使用 PyQt5 为 youtube 应用程序创建全屏模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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