【问题标题】:How to keep current URL updated in QWebKit browser如何在 QWebKit 浏览器中保持当前 URL 更新
【发布时间】:2019-09-11 13:49:09
【问题描述】:

我正在使用 PyQt5 WebKit 制作一个带有搜索框的非常简单的浏览器。这是我正在使用的代码:

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow, QGridLayout)
from PyQt5.QtWebKitWidgets import QWebView


class App(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        centralWidget   = QWidget()
        self.setCentralWidget(centralWidget)

        self.searchbox = QLineEdit("", self)
        self.go = QPushButton('Go', self)
        self.go.clicked.connect(self.gourl)  
        self.webview = Browser()        

        self.grid = QGridLayout(centralWidget)
        self.grid.addWidget(self.webview, 0, 0, 1, 4)
        self.grid.addWidget(self.searchbox, 1, 0)
        self.grid.addWidget(self.go, 1, 1)

    def gourl(self):
        url = self.searchbox.text()
        self.webview.load(QUrl(url))


class Browser(QWebView):   #(QWebView):
    windowList = []
    def createWindow(self, QWebEnginePage_WebWindowType):
        App.setCentralWidget(Browser())
        #new_window.show()
        self.windowList.append(App())  
        return Browser()

if __name__ == "__main__":    
    app = QApplication(sys.argv)
    box = App()
    box.setWindowTitle('Browser')
    box.resize(600, 500)
    box.show()
    sys.exit(app.exec_())

我想使用用户当前使用的任何 URL 来更新框中的 URL。我不知道该怎么做,任何帮助将不胜感激。

【问题讨论】:

    标签: python pyqt pyqt5 qwebkit qurl


    【解决方案1】:

    下面是对您的示例的重写,它应该可以满足您的需求。

    Browser 类已得到修复,以便在请求新窗口时创建App 的新实例。您可以通过右键单击链接并选择在新窗口中打开来测试它。每当更改时,搜索框都会自动更新为新的 url。

    import sys
    from PyQt5.QtCore import QUrl
    from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow, QGridLayout)
    from PyQt5.QtWebKitWidgets import QWebView
    
    
    class App(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            centralWidget   = QWidget()
            self.setCentralWidget(centralWidget)
    
            self.searchbox = QLineEdit("", self)
            self.go = QPushButton('Go', self)
            self.go.clicked.connect(self.gourl)
            self.webview = Browser()
            self.webview.urlChanged.connect(self.handleUrlChanged)
    
            self.grid = QGridLayout(centralWidget)
            self.grid.addWidget(self.webview, 0, 0, 1, 4)
            self.grid.addWidget(self.searchbox, 1, 0)
            self.grid.addWidget(self.go, 1, 1)
    
        def gourl(self):
            url = self.searchbox.text()
            self.webview.load(QUrl(url))
    
        def handleUrlChanged(self, url):
            self.searchbox.setText(url.toString())
    
    
    class Browser(QWebView):
        windowList = []
    
        def createWindow(self, wintype):
            window = App()
            self.windowList.append(window)
            window.show()
            return window.webview
    
        def closeEvent(self, event):
            self.windowList.remove(self)
            super().closeEvent(event)
    
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
        box = App()
        box.setWindowTitle('Browser')
        box.resize(600, 500)
        box.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 你必须检查 QLineEdit 文本是否是一个有效的 Url 或者会崩溃吗?
    • @Linux 不,它不会崩溃。它只是不会加载 url - 然后显示错误页面或 about:blank。但是你所说的“有效”到底是什么意思?如果您的意思是语法上有效,那么我想您可以使用url = QUrl.fromUserInput(text) 并检查url.isValid()。但是,如果您的意思是 语义上有效,那么就不可能知道 - http 请求可能随时返回 404 或 503(或其他)。试图提前弄清楚可能发生的事情真的是浪费时间——让浏览器处理它
    猜你喜欢
    • 1970-01-01
    • 2014-08-08
    • 2019-04-29
    • 1970-01-01
    • 2015-08-26
    • 2017-07-06
    • 2017-06-11
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多