PyQt window closes after launch

前端 未结 2 655
一向
一向 2021-01-17 03:53

I\'m trying to use a QPushButton to call a function that opens a new instance of QWebView. Works but as soon as the window opens it closes again. I\'ve read this - PyQt win

相关标签:
2条回答
  • 2021-01-17 04:55

    Create a MainWindow class that keeps a list of open Browsers, and every time when you open a browser, just add it to the list. And when a browser window closes, it will remove itself from the list, see closeEvent.

    import sys
    from PyQt4 import QtCore, QtGui, QtWebKit
    from PyQt4.QtWebKit import QWebSettings
    from PyQt4.QtNetwork import QNetworkAccessManager
    from PyQt4.QtNetwork import *
    
    
    UA_STRING = """Test Test Test""" 
    vidurl = ("empty")
    
    class YWebPage(QtWebKit.QWebPage):
        def __init__(self):
            super(YWebPage, self).__init__()
    
        def userAgentForUrl(self, url):
            return UA_STRING
    
    
    class Browser(QtGui.QMainWindow): # "Browser" window
        def __init__(self, main, url):
            QtGui.QMainWindow.__init__(self)
            self.main = main
            self.resize(800,600) # Viewport size
            self.webView = QtWebKit.QWebView()
            self.setCentralWidget(self.webView)
            self.yPage = YWebPage()
            self.webView.setPage(self.yPage)
            self.webView.load(QtCore.QUrl(url)) # Video URL
            self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True) # Enables flash player
    
        def closeEvent(self, event):
            self.main.browsers.remove(self)
            super(Browser, self).closeEvent(event)
    
    
    class MainWindow(QtGui.QWidget):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.browsers = []
    
            self.resize(200, 450)
            self.setFixedSize(200, 350)
            self.move(300, 300)
            self.setWindowTitle('U-bot 0.1')
    
            # Setup GUI
    
            # Start Button
            self.__button = QtGui.QPushButton('Start')
            self.__button.clicked.connect(self.open)
    
            # Text area
            self.__qle = QtGui.QLineEdit()
            self.__qle.setText("http://")
    
            # Images
            pixmap1 = QtGui.QPixmap("ubot.png")
            lbl1 = QtGui.QLabel()
            lbl1.resize(200, 150)
            lbl1.setPixmap(pixmap1)
            lbl1.setScaledContents(True)
    
            layout = QtGui.QVBoxLayout()
            layout.addStretch(1)
    
            layout.addWidget(self.__qle)
            layout.addWidget(self.__button)
    
            self.setLayout(layout)
    
        def open(self):
            b = Browser(self, self.__qle.text())
            b.show()
            self.browsers.append(b)
    
    
    def main():
        app = QtGui.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
  • 2021-01-17 04:58

    To keep a reference to a QObject, you can either keep the variable in scope, or add it as the child of another QObject which variable already stays in scope.

    And for QWidget, the parent should also be a QWidget, so, in your case, you'd want to make w as the parent of all your QMainWindows.

    def web1(parent):
        ...
        class Browser(QtGui.QMainWindow): # "Browser" window
            def __init__(self, parent):
                QtGui.QMainWindow.__init__(self, parent)
                ...
    
    def main():
        ...
        w.__button.clicked.connect(lambda: web1(w))
    

    This also avoid maintaining manually a list of opened windows, since the object hierarchy can do that for you.

    PS: The child windows are shown as toplevel windows and not inside the w window, because QMainWindow (and QDialog) has the Qt::Window flag set by default.

    0 讨论(0)
提交回复
热议问题