I am making a very simple browser with a search box using PyQt5 WebKit. This is the code I'm using:
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_())
I want to keep the URL in the box updated with whatever current URL the user is on. I have no idea how to go about this, any help would be appreciated.
Below is a re-write of your example which should do what you want.
The Browser
class has been fixed so that it creates a new instance of App
when a new window is requested. You can test this by right-clicking on a link and selecting Open in New Window. The search box will automatically update with the new url whenever it changes.
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_())