我正在使用PyQt5 QWebEngineView在窗口上显示一个网站,该网站支持诸如打开新窗口之类的基本功能。 我的代码对于window.open()可以正常工作,但是对于网页上的window.close()函数,它可以删除对该窗口的引用,但是除非用户手动关闭该窗口,否则该窗口实际上位于该窗口中。
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
class MyWindow(QtWebEngineWidgets.QWebEngineView):
currentFile = ''
def __init__(self,windows, parent=None):
super(MyWindow, self).__init__()
self._windows = windows
self._windows.append(self)
self.load(QUrl("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_close"))
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
self.show()
def createWindow(self, windows):
print(windows)
if windows == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
webView = MyWindow(self._windows)
webView.resize(900, 780) # <----
return webView
elif windows == QtWebEngineWidgets.QWebEnginePage.WebDialog:
webView = MyWindow(self._windows)
webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
webView.resize(900, 780) # <----
webView.show()
return webView
elif windows == QtWebEngineWidgets.QWebEnginePage.acceptNavigationRequest:
webView = MyWindow(self._windows)
webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
webView.resize(900, 780) # <----
webView.show()
return webView
return super(MyWindow, self).createWindow(windows)
if __name__ == "__main__":
app = QApplication(sys.argv)
windows = []
main = MyWindow(windows)
sys.exit(app.exec_())
当用户单击带有函数window.close()的按钮时,该窗口仍然存在,但已从堆栈中删除(我知道这会导致该页面无法正常工作)
答案 0 :(得分:0)
您必须为 QWebEngineView 设置一个 QWebEnginePage 并将 QWebEnginePage.windowCloseRequested 绑定到一个函数,如下所示:
import sys
import os
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
# from PyQt5.QtGui import QPixmap
# from PyQt5.QtGui import QDesktopServices
class MyWindow(QWebEngineView):
def __init__(self, windows, parent=None):
super(MyWindow, self).__init__()
self._windows = windows
self._windows.append(self)
# Path
scriptdir = os.path.dirname(os.path.realpath(__file__))
# Profile
self.profile = QWebEngineProfile().defaultProfile()
self.profile.setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
self.browser_storage_folder = scriptdir + os.path.sep + '/.cookies'
self.profile.setPersistentStoragePath(self.browser_storage_folder)
# Page
self.page = QWebEnginePage(self.profile, self)
self.setPage(self.page)
self.load(QUrl("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_close"))
self.setAttribute(Qt.WA_DeleteOnClose, True)
self.show()
def closeEvent(self, event):
print('Close Event')
def CloseRequest(self):
self.webView.close()
print('close')
def createWindow(self, windows):
print(windows)
if windows == QWebEnginePage.WebBrowserTab:
webView = MyWindow(self._windows)
webView.resize(900, 780)
return webView
elif windows == QWebEnginePage.WebDialog:
webView = MyWindow(self._windows)
webView.setAttribute(Qt.WA_DeleteOnClose, True)
page = QWebEnginePage(self)
page.windowCloseRequested.connect(self.CloseRequest)
webView.setPage(page)
webView.resize(900, 780)
webView.show()
self.webView = webView
return webView
elif windows == QWebEnginePage.acceptNavigationRequest:
webView = MyWindow(self._windows)
webView.setAttribute(Qt.WA_DeleteOnClose, True)
webView.resize(900, 780)
webView.show()
return webView
return super(MyWindow, self).createWindow(windows)
if __name__ == "__main__":
app = QApplication(sys.argv)
windows = []
main = MyWindow(windows)
sys.exit(app.exec_())