侧边栏壁纸
惠普电脑装Mac交流群群聊二维码.png
2023年02月25日 3.3k阅读 0评论 0点赞

准备环境
安装 pyqt5
终端里输入

pip install PyQt5

安装完成后创建一个test。py文件

import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView

class MainWindow(QMainWindow):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.setWindowTitle('浏览器标题')
    self.showMaximized()
    self.webview = WebEngineView()
    self.webview.load(QUrl("https://www.baidu.com"))
    self.setCentralWidget(self.webview)

class WebEngineView(QWebEngineView):
  windowList = []
  def createWindow(self, QWebEnginePage_WebWindowType):
    new_webview =  WebEngineView()
    new_window = MainWindow()
    new_window.setCentralWidget(new_webview)
    self.windowList.append(new_window)
    return new_webview

if __name__ == "__main__":
  app = QApplication(sys.argv)
  window = MainWindow()
  window.show()
  sys.exit(app.exec_())b

保存完毕直接执行test.py
16.png

如果出现报错 那是应为咱们安装的pyqt5版本太新了包里不包含PyQtWebEngine所以需要咱们主动安装一下

ModuleNotFoundError: No module named 'PyQt5.QtWebEngineWidgets'

安装 PyQtWebEngine

pip install PyQtWebEngine

安装完成就生成了一个浏览器

0

—— 评论区 ——

昵称
邮箱
网址
取消