pyqt实现简易浏览器

需要的软件包如下:

Package           Version
----------------- -------
pip               23.2.1
PyQt5             5.15.9
PyQt5-Qt5         5.15.2
PyQt5-sip         12.12.2
PyQtWebEngine     5.15.6
PyQtWebEngine-Qt5 5.15.2
setuptools        65.5.1
wheel             0.38.4

完整代码

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *


class SimpleWebBrowser(QMainWindow):
    def __init__(self):
        super().__init__()
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("http://www.google.com"))  # 设置默认网页
        self.setCentralWidget(self.browser)
        self.browser.urlChanged.connect(self.update_urlbar)
        self.browser.loadFinished.connect(self.update_title)

        navtb = QToolBar("Navigation")
        self.addToolBar(navtb)

        back_btn = QAction("Back", self)
        back_btn.setStatusTip("Back to previous page")
        back_btn.triggered.connect(self.browser.back)
        navtb.addAction(back_btn)

        next_btn = QAction("Forward", self)
        next_btn.setStatusTip("Forward to next page")
        next_btn.triggered.connect(self.browser.forward)
        navtb.addAction(next_btn)

        reload_btn = QAction("Reload", self)
        reload_btn.setStatusTip("Reload page")
        reload_btn.triggered.connect(self.browser.reload)
        navtb.addAction(reload_btn)

        home_btn = QAction("Home", self)
        home_btn.setStatusTip("Go home")
        home_btn.triggered.connect(self.navigate_home)
        navtb.addAction(home_btn)

        navtb.addSeparator()

        self.urlbar = QLineEdit()
        self.urlbar.returnPressed.connect(self.navigate_to_url)
        navtb.addWidget(self.urlbar)

        stop_btn = QAction("Stop", self)
        stop_btn.setStatusTip("Stop loading current page")
        stop_btn.triggered.connect(self.browser.stop)
        navtb.addAction(stop_btn)

        self.status = QStatusBar()
        self.setStatusBar(self.status)

        self.browser.page().titleChanged.connect(self.adjust_title)

        self.setGeometry(100, 100, 1024, 768)
        self.setWindowTitle("Simple Web Browser")

    def update_title(self):
        title = self.browser.page().title()
        self.setWindowTitle(title)

    def navigate_home(self):
        self.browser.setUrl(QUrl("http://www.google.com"))

    def navigate_to_url(self):
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
            q.setScheme("http")

        self.browser.setUrl(q)
        self.browser.setFocus()

    def update_urlbar(self, q):
        if q.scheme() == 'https':
            self.urlbar.setStyleSheet("color: green;")
        else:
            self.urlbar.setStyleSheet("color: black;")

        self.urlbar.setText(q.toString())
        self.urlbar.setCursorPosition(0)

    def adjust_title(self):
        self.setWindowTitle(self.browser.page().title())


def main():
    app = QApplication(sys.argv)
    QCoreApplication.setApplicationName("Simple Web Browser")
    window = SimpleWebBrowser()
    window.show()
    app.exec_()


if __name__ == "__main__":
    main()