PyQt5 window.close() does no close the window but window.open() works fine

拟墨画扇 提交于 2019-12-02 15:52:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!