如何在 pyqt5 webbrowser 中为 youtube 视频创建全屏模式
Posted
技术标签:
【中文标题】如何在 pyqt5 webbrowser 中为 youtube 视频创建全屏模式【英文标题】:How to create full screen mode for youtube videos in pyqt5 webbrowser 【发布时间】:2021-02-20 17:16:55 【问题描述】:我已经创建了用于在 Internet 上搜索的网络浏览器。
问题是,在 youtube 全屏网站上,我无法让它按照我想要的方式全屏运行,但是当我退出全屏模式时,我会看到两个窗口而不是一个,或者如果我卡住了一个窗口,我删除QWebEngineView。
我的 PyQt5 版本是 5.13.2
我的代码在下面
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os
import sys
# main window
class MainWindow(QMainWindow):
# constructor
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# creating a tab widget
self.tabs = QTabWidget()
# making document mode true
self.tabs.setDocumentMode(True)
# adding action when double clicked
self.tabs.tabBarDoubleClicked.connect(self.tab_open_doubleclick)
# adding action when tab is changed
self.tabs.currentChanged.connect(self.current_tab_changed)
# making tabs closeable
self.tabs.setTabsClosable(True)
# adding action when tab close is requested
self.tabs.tabCloseRequested.connect(self.close_current_tab)
# making tabs as central widget
self.setCentralWidget(self.tabs)
# creating a status bar
self.status = QStatusBar()
# setting status bar to the main window
self.setStatusBar(self.status)
# creating a tool bar for navigation
navtb = QToolBar("Navigation")
# adding tool bar tot he main window
self.addToolBar(navtb)
# creating back action
back_btn = QAction("Back", self)
# setting status tip
back_btn.setStatusTip("Back to previous page")
# adding action to back button
# making current tab to go back
back_btn.triggered.connect(lambda: self.tabs.currentWidget().back())
# adding this to the navigation tool bar
navtb.addAction(back_btn)
# similarly adding next button
next_btn = QAction("Forward", self)
next_btn.setStatusTip("Forward to next page")
next_btn.triggered.connect(lambda: self.tabs.currentWidget().forward())
navtb.addAction(next_btn)
# similarly adding reload button
reload_btn = QAction("Reload", self)
reload_btn.setStatusTip("Reload page")
reload_btn.triggered.connect(lambda: self.tabs.currentWidget().reload())
navtb.addAction(reload_btn)
# creating home action
home_btn = QAction("Home", self)
home_btn.setStatusTip("Go home")
# adding action to home button
home_btn.triggered.connect(self.navigate_home)
navtb.addAction(home_btn)
# adding a separator
navtb.addSeparator()
# creating a line edit widget for URL
self.urlbar = QLineEdit()
# adding action to line edit when return key is pressed
self.urlbar.returnPressed.connect(self.navigate_to_url)
# adding line edit to tool bar
navtb.addWidget(self.urlbar)
# similarly adding stop action
stop_btn = QAction("Stop", self)
stop_btn.setStatusTip("Stop loading current page")
stop_btn.triggered.connect(lambda: self.tabs.currentWidget().stop())
navtb.addAction(stop_btn)
# creating first tab
self.add_new_tab(QUrl('http://www.google.com'), 'Homepage')
# showing all the components
self.show()
# setting window title
self.setWindowTitle("Aashra")
# method for adding new tab
def add_new_tab(self, qurl = None, label ="Blank", text = "New Tab"):
# if url is blank
if qurl is None:
# creating a google url
qurl = QUrl('http://www.google.com')
# creating a QWebEngineView object
browser = QWebEngineView()
# setting url to browser
browser.setUrl(qurl)
# setting tab index
i = self.tabs.addTab(browser, label)
self.tabs.setCurrentIndex(i)
# adding action to the browser when url is changed
# update the url
browser.urlChanged.connect(lambda qurl, browser = browser:
self.update_urlbar(qurl, browser))
# adding action to the browser when loading is finished
# set the tab title
browser.loadFinished.connect(lambda _, i = i, browser = browser:
self.tabs.setTabText(i, browser.page().title()))
# when double clicked is pressed on tabs
def tab_open_doubleclick(self, i):
# checking index i.e
# No tab under the click
if i == -1:
# creating a new tab
self.add_new_tab()
# wen tab is changed
def current_tab_changed(self, i):
# get the curl
qurl = self.tabs.currentWidget().url()
# update the url
self.update_urlbar(qurl, self.tabs.currentWidget())
# update the title
self.update_title(self.tabs.currentWidget())
# when tab is closed
def close_current_tab(self, i):
# if there is only one tab
if self.tabs.count() < 2:
# do nothing
return
# else remove the tab
self.tabs.removeTab(i)
# method for updating the title
def update_title(self, browser):
# if signal is not from the current tab
if browser != self.tabs.currentWidget():
# do nothing
return
# get the page title
title = self.tabs.currentWidget().page().title()
# action to go to home
def navigate_home(self):
# go to google
self.tabs.currentWidget().setUrl(QUrl("http://www.google.com"))
# method for navigate to url
def navigate_to_url(self):
# get the line edit text
# convert it to QUrl object
q = QUrl(self.urlbar.text())
# if scheme is blank
if q.scheme() == "":
# set scheme
q.setScheme("http")
# set the url
self.tabs.currentWidget().setUrl(q)
# method to update the url
def update_urlbar(self, q, browser = None):
# If this signal is not from the current tab, ignore
if browser != self.tabs.currentWidget():
return
# set text to the url bar
self.urlbar.setText(q.toString())
# set cursor position
self.urlbar.setCursorPosition(0)
def _downloadRequested(item): # QWebEngineDownloadItem
print('downloading to', item.path())
item.accept()
browser.page().profile().downloadRequested.connect(_downloadRequested)
# creating a PyQt5 application
app = QApplication(sys.argv)
# setting name to the application
app.setApplicationName("Aashra")
# creating MainWindow object
window = MainWindow()
# loop
app.exec_()
【问题讨论】:
【参考方案1】:您必须激活 FullScreenSupportEnabled 标志,以便您可以使用 fullScreenRequested 信号,该信号将在浏览器需要窗口更改状态时发出通知。在连接到 fullScreenRequested 的方法中,您必须使用 showFullScreen() 并在请求的 toggleOn() 为 True 时隐藏其他小部件,使用 showNormal() 并在为 false 时显示隐藏的小部件。
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os
import sys
# main window
class MainWindow(QMainWindow):
# constructor
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# creating a tab widget
self.tabs = QTabWidget()
# making document mode true
self.tabs.setDocumentMode(True)
# adding action when double clicked
self.tabs.tabBarDoubleClicked.connect(self.tab_open_doubleclick)
# adding action when tab is changed
self.tabs.currentChanged.connect(self.current_tab_changed)
# making tabs closeable
self.tabs.setTabsClosable(True)
# adding action when tab close is requested
self.tabs.tabCloseRequested.connect(self.close_current_tab)
# making tabs as central widget
self.setCentralWidget(self.tabs)
# creating a status bar
self.status = QStatusBar()
# setting status bar to the main window
self.setStatusBar(self.status)
# creating a tool bar for navigation
self.navtb = QToolBar("Navigation")
# adding tool bar tot he main window
self.addToolBar(self.navtb)
# creating back action
back_btn = QAction("Back", self)
# setting status tip
back_btn.setStatusTip("Back to previous page")
# adding action to back button
# making current tab to go back
back_btn.triggered.connect(lambda: self.tabs.currentWidget().back())
# adding this to the navigation tool bar
self.navtb.addAction(back_btn)
# similarly adding next button
next_btn = QAction("Forward", self)
next_btn.setStatusTip("Forward to next page")
next_btn.triggered.connect(lambda: self.tabs.currentWidget().forward())
self.navtb.addAction(next_btn)
# similarly adding reload button
reload_btn = QAction("Reload", self)
reload_btn.setStatusTip("Reload page")
reload_btn.triggered.connect(lambda: self.tabs.currentWidget().reload())
self.navtb.addAction(reload_btn)
# creating home action
home_btn = QAction("Home", self)
home_btn.setStatusTip("Go home")
# adding action to home button
home_btn.triggered.connect(self.navigate_home)
self.navtb.addAction(home_btn)
# adding a separator
self.navtb.addSeparator()
# creating a line edit widget for URL
self.urlbar = QLineEdit()
# adding action to line edit when return key is pressed
self.urlbar.returnPressed.connect(self.navigate_to_url)
# adding line edit to tool bar
self.navtb.addWidget(self.urlbar)
# similarly adding stop action
stop_btn = QAction("Stop", self)
stop_btn.setStatusTip("Stop loading current page")
stop_btn.triggered.connect(lambda: self.tabs.currentWidget().stop())
self.navtb.addAction(stop_btn)
# creating first tab
self.add_new_tab(QUrl("http://www.google.com"), "Homepage")
# showing all the components
self.show()
# setting window title
self.setWindowTitle("Aashra")
# method for adding new tab
def add_new_tab(self, qurl=None, label="Blank", text="New Tab"):
# if url is blank
if qurl is None:
# creating a google url
qurl = QUrl("http://www.google.com")
# creating a QWebEngineView object
browser = QWebEngineView()
browser.settings().setAttribute(
QWebEngineSettings.FullScreenSupportEnabled, True
)
# setting url to browser
browser.setUrl(qurl)
# setting tab index
i = self.tabs.addTab(browser, label)
self.tabs.setCurrentIndex(i)
# adding action to the browser when url is changed
# update the url
browser.urlChanged.connect(
lambda qurl, browser=browser: self.update_urlbar(qurl, browser)
)
# adding action to the browser when loading is finished
# set the tab title
browser.loadFinished.connect(
lambda _, i=i, browser=browser: self.tabs.setTabText(
i, browser.page().title()
)
)
browser.page().fullScreenRequested.connect(
lambda request, browser=browser: self.handle_fullscreen_requested(
request, browser
)
)
def handle_fullscreen_requested(self, request, browser):
request.accept()
if request.toggleOn():
self.showFullScreen()
self.statusBar().hide()
self.navtb.hide()
self.tabs.tabBar().hide()
else:
self.showNormal()
self.statusBar().show()
self.navtb.show()
self.tabs.tabBar().show()
# when double clicked is pressed on tabs
def tab_open_doubleclick(self, i):
# checking index i.e
# No tab under the click
if i == -1:
# creating a new tab
self.add_new_tab()
# wen tab is changed
def current_tab_changed(self, i):
# get the curl
qurl = self.tabs.currentWidget().url()
# update the url
self.update_urlbar(qurl, self.tabs.currentWidget())
# update the title
self.update_title(self.tabs.currentWidget())
# when tab is closed
def close_current_tab(self, i):
# if there is only one tab
if self.tabs.count() < 2:
# do nothing
return
# else remove the tab
self.tabs.removeTab(i)
# method for updating the title
def update_title(self, browser):
# if signal is not from the current tab
if browser != self.tabs.currentWidget():
# do nothing
return
# get the page title
title = self.tabs.currentWidget().page().title()
# action to go to home
def navigate_home(self):
# go to google
self.tabs.currentWidget().setUrl(QUrl("http://www.google.com"))
# method for navigate to url
def navigate_to_url(self):
# get the line edit text
# convert it to QUrl object
q = QUrl(self.urlbar.text())
# if scheme is blank
if q.scheme() == "":
# set scheme
q.setScheme("http")
# set the url
self.tabs.currentWidget().setUrl(q)
# method to update the url
def update_urlbar(self, q, browser=None):
# If this signal is not from the current tab, ignore
if browser != self.tabs.currentWidget():
return
# set text to the url bar
self.urlbar.setText(q.toString())
# set cursor position
self.urlbar.setCursorPosition(0)
def _downloadRequested(item): # QWebEngineDownloadItem
print("downloading to", item.path())
item.accept()
browser.page().profile().downloadRequested.connect(_downloadRequested)
# creating a PyQt5 application
app = QApplication(sys.argv)
# setting name to the application
app.setApplicationName("Aashra")
# creating MainWindow object
window = MainWindow()
# loop
app.exec_()
【讨论】:
+1 感谢您提供的信息丰富的答案。不过我想知道,是否可以发送错误的关闭全屏请求?我已经将esc
键绑定到一个函数,但是当我只是输入代码以关闭全屏时,什么也没有发生。所以我想尝试发送一个错误的请求。 request.toggleOff()
会起作用吗?以上是关于如何在 pyqt5 webbrowser 中为 youtube 视频创建全屏模式的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PyQt5 中为 QGridLayout 添加拉伸?
在 Ubuntu 14.04 中为 Python3.4 构建 PyQt5 的问题是由 qprinter.h 引起的,然后 pyuic5 错误