如何在 QWebKit 浏览器中保持当前 URL 更新

How to keep current URL updated in QWebKit browser

我正在使用 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 保持同步。我不知道该怎么做,任何帮助将不胜感激。

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

Browser class 已修复,当请求新的 window 时,它会创建 App 的新实例。您可以通过右键单击 link 并选择 Open in New Window 来对此进行测试。搜索框会在更改时自动更新为新的 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_())