I'm using PyQt5 QWebEngineView to show a website on the window that supports basic functionalities like opening a new window. My code works fine for window.open() but for window.close() function on a webpage it removes the reference to the window but the window is physically there unless the user manually closes the window.
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_())
When the user clicks on the button with function window.close() the window is still there but is refrenced from the stack (I know this cause that page does not work)
来源:https://stackoverflow.com/questions/56405096/pyqt5-window-close-does-no-close-the-window-but-window-open-works-fine