PyQt5 window.close() 不会关闭窗口,但 window.open() 工作正常
Posted
技术标签:
【中文标题】PyQt5 window.close() 不会关闭窗口,但 window.open() 工作正常【英文标题】:PyQt5 window.close() does no close the window but window.open() works fine 【发布时间】:2019-06-01 08:31:37 【问题描述】:我正在使用 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() 的按钮时,窗口仍然存在,但会从堆栈中引用(我知道这是因为该页面无法正常工作)
【问题讨论】:
此代码中没有方法open
或close
。我不明白你在说什么。
它是一个用于打开和关闭新窗口的javascript函数。
【参考方案1】:
您必须为 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_())
【讨论】:
以上是关于PyQt5 window.close() 不会关闭窗口,但 window.open() 工作正常的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript中window.open()打开与window.close()关闭
火狐浏览器中如何使用javascript:window.close()关闭窗口?