QWebView 不会显示网页

QWebView Wont Display Webpage

我最近在 PyCharm 中使用 PyQt4 创建了一个网络浏览器,当我 运行 我的网络浏览器弹出程序时,我输入:

http://www.google.com

然而,屏幕仍然是白色的。

下面是代码:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1269, 670)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet(_fromUtf8("background: url(:/path/to-your-resource.png)"))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tb_back = QtGui.QToolButton(self.centralwidget)
        self.tb_back.setGeometry(QtCore.QRect(10, 20, 41, 31))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/back.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_back.setIcon(icon)
        self.tb_back.setIconSize(QtCore.QSize(32, 32))
        self.tb_back.setObjectName(_fromUtf8("tb_back"))
        self.tb_reload = QtGui.QToolButton(self.centralwidget)
        self.tb_reload.setGeometry(QtCore.QRect(60, 20, 41, 31))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/reload.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_reload.setIcon(icon1)
        self.tb_reload.setIconSize(QtCore.QSize(32, 32))
        self.tb_reload.setObjectName(_fromUtf8("tb_reload"))
        self.tb_forword_2 = QtGui.QToolButton(self.centralwidget)
        self.tb_forword_2.setGeometry(QtCore.QRect(110, 20, 41, 31))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/forword.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_forword_2.setIcon(icon2)
        self.tb_forword_2.setIconSize(QtCore.QSize(32, 32))
        self.tb_forword_2.setObjectName(_fromUtf8("tb_forword_2"))
        self.tb_home = QtGui.QToolButton(self.centralwidget)
        self.tb_home.setGeometry(QtCore.QRect(160, 20, 41, 31))
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/home.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_home.setIcon(icon3)
        self.tb_home.setIconSize(QtCore.QSize(32, 32))
        self.tb_home.setObjectName(_fromUtf8("tb_home"))
        self.ln_addressbar = QtGui.QLineEdit(self.centralwidget)
        self.ln_addressbar.setGeometry(QtCore.QRect(210, 20, 491, 31))
        self.ln_addressbar.setObjectName(_fromUtf8("ln_addressbar"))
        self.tb_search = QtGui.QToolButton(self.centralwidget)
        self.tb_search.setGeometry(QtCore.QRect(710, 20, 41, 31))
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/search.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_search.setIcon(icon4)
        self.tb_search.setIconSize(QtCore.QSize(32, 32))
        self.tb_search.setObjectName(_fromUtf8("tb_search"))
        self.webView = QtWebKit.QWebView(self.centralwidget)
        self.webView.setGeometry(QtCore.QRect(0, 60, 1261, 591))
        self.webView.setUrl(QtCore.QUrl(_fromUtf8("about:blank")))
        self.webView.setObjectName(_fromUtf8("webView"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Neel\'s Web Browser", None))
        self.tb_back.setText(_translate("MainWindow", "...", None))
        self.tb_reload.setText(_translate("MainWindow", "...", None))
        self.tb_forword_2.setText(_translate("MainWindow", "...", None))
        self.tb_home.setText(_translate("MainWindow", "...", None))
        self.tb_search.setText(_translate("MainWindow", "...", None))

from PyQt4 import QtWebKit


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

为了解决这个问题我尝试了

www.google.com

但问题依然存在。

如果这有帮助,我会在 PyCharm 社区应用程序中 运行 这段代码。

问题是,即使你放置了 url,QWebView 也不知道你已经放置了它,为此你必须使用一个信号(在这种情况下 editingFinished, 当你按下 enter 或 return 键时执行),另一个问题是你需要添加前缀:http://

self.ln_addressbar.editingFinished.connec(self.OnEditingFinished)

def OnEditingFinished(self):
    self.webView.setUrl(QtCore.QUrl("http://"+self.ln_addressbar.text()))

完整代码:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1269, 670)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet(_fromUtf8("background: url(:/path/to-your-resource.png)"))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tb_back = QtGui.QToolButton(self.centralwidget)
        self.tb_back.setGeometry(QtCore.QRect(10, 20, 41, 31))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/back.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_back.setIcon(icon)
        self.tb_back.setIconSize(QtCore.QSize(32, 32))
        self.tb_back.setObjectName(_fromUtf8("tb_back"))
        self.tb_reload = QtGui.QToolButton(self.centralwidget)
        self.tb_reload.setGeometry(QtCore.QRect(60, 20, 41, 31))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/reload.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_reload.setIcon(icon1)
        self.tb_reload.setIconSize(QtCore.QSize(32, 32))
        self.tb_reload.setObjectName(_fromUtf8("tb_reload"))
        self.tb_forword_2 = QtGui.QToolButton(self.centralwidget)
        self.tb_forword_2.setGeometry(QtCore.QRect(110, 20, 41, 31))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/forword.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_forword_2.setIcon(icon2)
        self.tb_forword_2.setIconSize(QtCore.QSize(32, 32))
        self.tb_forword_2.setObjectName(_fromUtf8("tb_forword_2"))
        self.tb_home = QtGui.QToolButton(self.centralwidget)
        self.tb_home.setGeometry(QtCore.QRect(160, 20, 41, 31))
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/home.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_home.setIcon(icon3)
        self.tb_home.setIconSize(QtCore.QSize(32, 32))
        self.tb_home.setObjectName(_fromUtf8("tb_home"))
        self.ln_addressbar = QtGui.QLineEdit(self.centralwidget)
        self.ln_addressbar.setGeometry(QtCore.QRect(210, 20, 491, 31))
        self.ln_addressbar.setObjectName(_fromUtf8("ln_addressbar"))

        self.ln_addressbar.editingFinished.connec(self.OnEditingFinished)
        self.tb_search = QtGui.QToolButton(self.centralwidget)
        self.tb_search.setGeometry(QtCore.QRect(710, 20, 41, 31))
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/icons/search.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tb_search.setIcon(icon4)
        self.tb_search.setIconSize(QtCore.QSize(32, 32))
        self.tb_search.setObjectName(_fromUtf8("tb_search"))
        self.webView = QtWebKit.QWebView(self.centralwidget)
        self.webView.setGeometry(QtCore.QRect(0, 60, 1261, 591))
        self.webView.setUrl(QtCore.QUrl(_fromUtf8("about:blank")))
        self.webView.setObjectName(_fromUtf8("webView"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Neel\'s Web Browser", None))
        self.tb_back.setText(_translate("MainWindow", "...", None))
        self.tb_reload.setText(_translate("MainWindow", "...", None))
        self.tb_forword_2.setText(_translate("MainWindow", "...", None))
        self.tb_home.setText(_translate("MainWindow", "...", None))
        self.tb_search.setText(_translate("MainWindow", "...", None))

    def OnEditingFinished(self):
        self.webView.setUrl(QtCore.QUrl("http://"+self.ln_addressbar.text()))

from PyQt4 import QtWebKit


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())