网站建设项目的运行与测试报告链接推广平台
Pyside6是由Qt官方维护和开发的一个用于创建跨平台桌面应用程序的Python绑定库。QtWebEngine是Qt提供的一个模块,它基于Chromium项目,允许开发者在他们的应用程序中嵌入网页内容。通过结合Pyside6和QtWebEngine,开发者可以轻松地创建具有现代网页浏览功能的桌面应用程序。本文,我将向大家介绍QtWebEngine的使用方法。
PyQt5 Vs Pyside6
谈到Qt,大家最熟悉的莫过于C++中的Qt了,当然,今天我们在Python中常用的两个Qt库其本身也是C++中Qt的API,运行时需要调用自身的一些dll文件。这里我给大家介绍一下PyQt5与PySide6这两个库的区别。
对比项 | PyQt5 | PySide6 |
---|---|---|
开发公司 | Riverbank Computing | Qt 官方 (The Qt Company) |
许可证 | GPL 或商业许可证 | LGPL 或商业许可证 |
Python 绑定 | 由 Riverbank 维护 | 由 Qt 官方维护 |
Qt 版本支持 | Qt 5 | Qt 6 |
API 兼容性 | 与 Qt5 完全兼容 | 与 Qt6 完全兼容 |
模块命名 | PyQt5.QtCore , PyQt5.QtWidgets | PySide6.QtCore , PySide6.QtWidgets |
信号与槽语法 | pyqtSignal , pyqtSlot | Signal , Slot |
多线程支持 | 支持 | 支持 |
文档与社区 | 文档丰富,社区活跃 | 官方文档完善,社区逐渐增长 |
商业支持 | 需购买商业许可证 | 可直接闭源商用 |
安装方式 | pip install PyQt5 | pip install PySide6 |
性能 | 相近 | 相近 |
与 C++ Qt 兼容性 | 高 | 高 |
未来维护 | 仍活跃,但主要支持 Qt5 | Qt 官方维护,未来更倾向 Qt6 |
总而言之,Pyside6的LGPL证书允许闭源(LGPL指不修改其dll文件中的底层代码基础上允许闭源),且对于Qt6的支持更先进,大家熟知的Spyder IDE就是基于Pyqt5开发,目前正在逐步完善支持Pyside6。
Spyder部分源码
Pyside 获取方式
pip install PySide6
QtWebEngine
QtWebEngine是Qt提供的一个模块,它基于Chromium项目,允许开发者在他们的应用程序中需通过浏览器便可嵌入网页内容,通过结合Pyside6和QtWebEngine,开发者可以轻松地创建具有现代网页浏览功能的桌面应用程序。
这里我们以嵌入东方财经网的股票指数为例:
思路分析
A股目前最常用的13个股指分别是:
{'上证指数':'000001','深证成指':'399001','创业板指':'399006',
'科创50':'000688','中证A50':'930050','中证A500':'000510','沪深300':'000300',
'中证500':'399905','中证800':'000906','中证1000':'000852','中证2000':'932000',
'上证50':'000016','北证50':'899050'}
这些股指在东方财富网中,除了中证2000与中证A50外Url都是固定的格式:
"https://quote.eastmoney.com/zs指数股票代码.html#fullScreenChart"
中证2000与中证A50的Url略有不同:
"https://quote.eastmoney.com/zz/2.指数股票代码.html#fullScreenChart"
https://quote.eastmoney.com/zs000001.html#fullScreenCharthttps://quote.eastmoney.com/zs000001.html#fullScreenChart
东方财富网上证指数
接下来,我们要做的便是根据给定的股指名称将其转换为Url
indexName_to_code={'上证指数':'000001','深证成指':'399001','创业板指':'399006','科创50':'000688','中证A50':'930050','中证A500':'000510','沪深300':'000300','中证500':'399905','中证800':'000906','中证1000':'000852','中证2000':'932000','上证50':'000016','北证50':'899050'}if indexName=='中证A50' or indexName=='中证2000':url=f"https://quote.eastmoney.com/zz/2.{indexName_to_code[indexName]}.html#fullScreenChart"
else:url=f"https://quote.eastmoney.com/zs{indexName_to_code[indexName]}.html#fullScr
Webbrowser类定义
接着我们来使用QTwebengine来自定义一个Webbrowser类:
import sys
import os
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtCore import QUrlclass WebBrowser(QMainWindow):def __init__(self,indexName):super().__init__()self.indexName=indexNameindexName_to_code={'上证指数':'000001','深证成指':'399001','创业板指':'399006','科创50':'000688','中证A50':'930050','中证A500':'000510','沪深300':'000300','中证500':'399905','中证800':'000906','中证1000':'000852','中证2000':'932000','上证50':'000016','北证50':'899050'}if indexName=='中证A50' or indexName=='中证2000':self.url=f"https://quote.eastmoney.com/zz/2.{indexName_to_code[self.indexName]}.html#fullScreenChart"else:self.url=f"https://quote.eastmoney.com/zs{indexName_to_code[self.indexName]}.html#fullScreenChart"self.setup_ui()def setup_ui(self):self.setWindowTitle(self.indexName)self.resize(1024,768)central_widget = QWidget()self.setCentralWidget(central_widget)layout=QVBoxLayout(central_widget)layout.setContentsMargins(0, 0, 0, 0)try:self.webview=QWebEngineView()layout.addWidget(self.webview)self.webview.setUrl(QUrl(self.url))except Exception:#如果没有GPU的话就#强制使用CPU渲染os.environ["QT_QUICK_BACKEND"] = "software" os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--disable-gpu --disable-software-rasterizer"self.webview=QWebEngineView()layout.addWidget(self.webview)self.webview.setUrl(QUrl(self.url))def closeEvent(self, event):# 彻底释放资源self.webview.stop()self.webview.page().profile().clearHttpCache()self.webview.setPage(None)self.webview.deleteLater()QApplication.processEvents() event.accept()
接着,我们需要按照规范的格式来启动Qt程序
def open_kline_window(index_name):if not QApplication.instance():qt_app=QApplication(sys.argv)# 创建并显示K线窗口current_qt_window=WebBrowser(index_name)current_qt_window.show()qt_app.exec()
完整代码
import sys
import os
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtCore import QUrlclass WebBrowser(QMainWindow):def __init__(self,indexName):super().__init__()self.indexName=indexNameindexName_to_code={'上证指数':'000001','深证成指':'399001','创业板指':'399006','科创50':'000688','中证A50':'930050','中证A500':'000510','沪深300':'000300','中证500':'399905','中证800':'000906','中证1000':'000852','中证2000':'932000','上证50':'000016','北证50':'899050'}if indexName=='中证A50' or indexName=='中证2000':self.url=f"https://quote.eastmoney.com/zz/2.{indexName_to_code[self.indexName]}.html#fullScreenChart"else:self.url=f"https://quote.eastmoney.com/zs{indexName_to_code[self.indexName]}.html#fullScreenChart"self.setup_ui()def setup_ui(self):self.setWindowTitle(self.indexName)self.resize(1024,768)central_widget = QWidget()self.setCentralWidget(central_widget)layout=QVBoxLayout(central_widget)layout.setContentsMargins(0, 0, 0, 0)try:self.webview=QWebEngineView()layout.addWidget(self.webview)self.webview.setUrl(QUrl(self.url))except Exception:#如果没有GPU的话就#强制使用CPU渲染os.environ["QT_QUICK_BACKEND"] = "software" os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--disable-gpu --disable-software-rasterizer"self.webview=QWebEngineView()layout.addWidget(self.webview)self.webview.setUrl(QUrl(self.url))def closeEvent(self, event):# 彻底释放资源self.webview.stop()self.webview.page().profile().clearHttpCache()self.webview.setPage(None)self.webview.deleteLater()QApplication.processEvents() event.accept()def open_kline_window(index_name):if not QApplication.instance():qt_app=QApplication(sys.argv)# 创建并显示K线窗口current_qt_window=WebBrowser(index_name)current_qt_window.show()qt_app.exec()
open_kline_window('上证指数')