0

我正在尝试使用 QPushButton 来调用打开 QWebView 新实例的函数。有效,但一旦窗口打开,它就会再次关闭。我读过这个——PyQt 窗口在打开后立即关闭,但我不明白如何引用窗口以使其保持打开状态。

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")

def web1():

    class YWebPage(QtWebKit.QWebPage):
        def __init__(self):
            super(QtWebKit.QWebPage, self).__init__()

        def userAgentForUrl(self, url):
            return UA_STRING


    class Browser(QtGui.QMainWindow): # "Browser" window


        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            self.resize(800,600) # Viewport size
            self.centralwidget = QtGui.QWidget(self)
            self.html = QtWebKit.QWebView()


        def browse(self):
            self.webView = QtWebKit.QWebView()
            self.yPage = YWebPage()
            self.webView.setPage(self.yPage)
            self.webView.load(QtCore.QUrl(vidurl)) # Video URL
            self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True) # Enables flash player
            self.webView.show()

    x = Browser()
    # QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.HttpProxy, "proxy.example.com", 8080)) # Proxy setting
    x.browse()



def main(): # Dialog window

    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()

    w.resize(200, 450)
    w.setFixedSize(200, 350)
    w.move(300, 300)
    w.setWindowTitle('U-bot 0.1')

    # Setup GUI

    # Start Button
    w.__button = QtGui.QPushButton(w)
    w.__button.clicked.connect(lambda: web1())

    # Text area
    w.__qle = QtGui.QLineEdit(w)
    w.__qle.setText ("http://")
    vidurl = w.__qle.text # Get video url from user

    # Images
    pixmap1 = QtGui.QPixmap("ubot.png")
    lbl1 = QtGui.QLabel(w)
    lbl1.resize(200, 150)
    lbl1.setPixmap(pixmap1)
    lbl1.setScaledContents(True)

    w.__button.setText('Start')
    layout = QtGui.QVBoxLayout()
    layout.addStretch(1)

    layout.addWidget(w.__qle)
    layout.addWidget(w.__button)


    w.setLayout(layout)
    w.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
    app.exec_()
4

2 回答 2

5

创建一个MainWindow保存打开浏览器列表的类,每次打开浏览器时,只需将其添加到列表中即可。当浏览器窗口关闭时,它会从列表中删除自己,请参阅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()
于 2013-08-05T15:14:52.740 回答
4

要保留对 a 的引用QObject,您可以将变量保留在范围内,或者将其添加为另一个变量的子级,QObject该变量已经在范围内。

而对于QWidget,父母也应该是 a QWidget,因此,在您的情况下,您希望将其设为所有sw的父母。QMainWindow

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))

这也避免了手动维护打开的窗口列表,因为对象层次结构可以为您做到这一点。

PS:子窗口显示为顶层窗口而不是w窗口内部,因为QMainWindow(and QDialog)Qt::Window默认设置了标志。

于 2013-08-05T15:34:57.057 回答