当前位置:   article > 正文

PyQt6 与 Pyecharts 交互:增加进度条显示网页加载进度_pyqt6 qwebengineview

pyqt6 qwebengineview

1. 官方文档

QWebEngineView — PyQt Documentation v6.6.0

2. 基于QProgressBar

2.1. 效果展示

网页加载过程中,会显示进度条

网页加载完毕后,进度条部分隐藏,整个页面将完全显示网页内容。 

2.2 文件1:Ui界面 <fig_progress.py>

2.2.1 Qt Designer界面

2.2.2 代码内容

关键代码:self.widget_figure = QtWebEngineWidgets.QWebEngineView(parent=Dialog)

注意,显示网页使用的是QWebEngineView,可以通过Qt Designer的提升功能实现,关于pyqt显示pyecharts图的操作细节请参考下文:

PyQt6与Pyecharts交互_pyside6 pyqt6 pyecharts-CSDN博客

  1. # Form implementation generated from reading ui file 'fig_progress.ui'
  2. #
  3. # Created by: PyQt6 UI code generator 6.6.1
  4. #
  5. # WARNING: Any manual changes made to this file will be lost when pyuic6 is
  6. # run again. Do not edit this file unless you know what you are doing.
  7. from PyQt6 import QtCore, QtGui, QtWidgets
  8. from PyQt6 import QtWebEngineWidgets
  9. class Ui_Dialog(object):
  10. def setupUi(self, Dialog):
  11. Dialog.setObjectName("Dialog")
  12. Dialog.resize(749, 463)
  13. self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
  14. self.verticalLayout.setObjectName("verticalLayout")
  15. self.verticalLayout_all = QtWidgets.QVBoxLayout()
  16. self.verticalLayout_all.setObjectName("verticalLayout_all")
  17. self.widget_figure = QtWebEngineWidgets.QWebEngineView(parent=Dialog)
  18. self.widget_figure.setObjectName("widget_figure")
  19. self.verticalLayout_all.addWidget(self.widget_figure)
  20. self.frame_progress = QtWidgets.QFrame(parent=Dialog)
  21. self.frame_progress.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
  22. self.frame_progress.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
  23. self.frame_progress.setObjectName("frame_progress")
  24. self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.frame_progress)
  25. self.verticalLayout_3.setObjectName("verticalLayout_3")
  26. self.verticalLayout_fig_progress = QtWidgets.QVBoxLayout()
  27. self.verticalLayout_fig_progress.setObjectName("verticalLayout_fig_progress")
  28. self.label = QtWidgets.QLabel(parent=self.frame_progress)
  29. font = QtGui.QFont()
  30. font.setPointSize(10)
  31. self.label.setFont(font)
  32. self.label.setObjectName("label")
  33. self.verticalLayout_fig_progress.addWidget(self.label)
  34. self.progressBar = QtWidgets.QProgressBar(parent=self.frame_progress)
  35. self.progressBar.setProperty("value", 24)
  36. self.progressBar.setObjectName("progressBar")
  37. self.verticalLayout_fig_progress.addWidget(self.progressBar)
  38. self.verticalLayout_3.addLayout(self.verticalLayout_fig_progress)
  39. self.verticalLayout_3.setStretch(0, 2)
  40. self.verticalLayout_all.addWidget(self.frame_progress)
  41. self.verticalLayout_all.setStretch(0, 10)
  42. self.verticalLayout_all.setStretch(1, 2)
  43. self.verticalLayout.addLayout(self.verticalLayout_all)
  44. self.retranslateUi(Dialog)
  45. QtCore.QMetaObject.connectSlotsByName(Dialog)
  46. def retranslateUi(self, Dialog):
  47. _translate = QtCore.QCoreApplication.translate
  48. Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
  49. self.label.setText(_translate("Dialog", "图像加载中,请等待..."))

2.3 文件2:主程序 <fig_progress_main.py>

增加进度条显示网页加载进度。

  1. import sys
  2. import fig_progress
  3. from PyQt6.QtWidgets import QApplication, QDialog
  4. from PyQt6.QtCore import QUrl, QFileInfo
  5. class Ui_CDSN(fig_progress.Ui_Dialog, QDialog):
  6. def __init__(self):
  7. super().__init__()
  8. self.setupUi(self)
  9. self.frame_progress.hide()
  10. self.widget_figure.page().loadProgress.connect(self.on_load_progress)
  11. url = QUrl("file:" + QFileInfo("my_figure.html").absoluteFilePath())
  12. self.widget_figure.load(url)
  13. self.show()
  14. def on_load_progress(self, progress):
  15. self.frame_progress.show()
  16. self.progressBar.setValue(progress)
  17. if progress == 100:
  18. self.frame_progress.hide()
  19. if __name__ == '__main__':
  20. app = QApplication(sys.argv)
  21. ui = Ui_CDSN()
  22. app.exec()

3. 基于QProgressDialog

4. 参考文档

关于QProgressBar,更多细节可参考

PyQt6基础操作:进度条QProgressBar-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/570806
推荐阅读
相关标签
  

闽ICP备14008679号