分享

【PyQt5

 lucifer76 2020-03-24

先用纯Python代码写一个简单的小案例:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout,QFrame
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys
class Stacked(QWidget):
    def __init__(self):
        super(Stacked, self).__init__()
        self.initUI()
        self.mainLayout()
    def initUI(self):
        self.setGeometry(400,400,800,600)
        self.setWindowTitle("demo1")
    def mainLayout(self):
        self.mainhboxLayout = QHBoxLayout(self)
        self.frame = QFrame(self)
        self.mainhboxLayout.addWidget(self.frame)
        self.hboxLayout = QHBoxLayout(self.frame)
        self.myHtml = QWebEngineView()
        url = "http://www.baidu.com"
        #打开本地html文件
        self.myHtml.load(QUrl("file:///D:/360Downloads/Python编程/数据分析+数据可视化/pyecharts/PyQt5+pyecharts/bar1.html"))
        # self.myHtml.load(QUrl("bar1.html"))   #无法显示,要使用绝对地址定位,在地址前面加上 file:/// ,将地址的 \ 改为/
        #打开网页url
        # self.myHtml.load(QUrl(url))
         
        self.hboxLayout.addWidget(self.myHtml)
        self.setLayout(self.mainhboxLayout)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Stacked()
    ex.show()
    sys.exit(app.exec_())

案例2:使用Eric6-Qtdesigner完成界面,再重写事件响应

效果如下:

参考:

 https://www.cnblogs.com/jyroy/p/9457882.html
https://blog.csdn.net/La_vie_est_belle/article/details/84837174
https://www.cnblogs.com/zxpo/p/7562968.html

ps:

  用的结构是左边一个QListWidget + 右边QStackedWidget,将QWebEngineView加在QStackedWidget里面的QFrame里面(用的pyecharts做的数据可视化 生成的html文件,直接嵌入在GUI界面中)

注意:

PyQt5 最新版本之后统一使用 : 

from PyQt5.QtWebEngineWidgets import QWebEngineView

试了很多次 QStackedWidget.addWidget(self.browser)  总是出错,故在QStackedWidget 里面嵌套了一个QFrame,再把self.browser 添加到QFrame里面

解决 No module named PyQt5.QtWebKitWidgets
原因:在 PyQt 5.6(+) 版本中, 新增 QtWebEngineWidgets 代替QtWebKitWidgets。

完整代码:

界面代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
from PyQt5.QtCore import pyqtSlot,QUrl
from PyQt5.QtWidgets import QDialog, QApplication,QHBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
from Ui_Stack_Widget import Ui_Dialog
import sys
class Dialog(QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        frame_list = [self.frame_1,self.frame_2,self.frame_3,self.frame_4,self.frame_5,self.frame_6,self.frame_7,self.frame_8,self.frame_9,self.frame_10]
        for i in range(10):
            self.browser = QWebEngineView(self)
            self.browser.load(QUrl("file:///D:/360Downloads/Python编程/数据分析+数据可视化/pyecharts/PyQt5+pyecharts/Qt设计师/{}.html".format(str(i))))
            hboxlayout = QHBoxLayout(frame_list[i])
            hboxlayout.addWidget(self.browser)
        self.listWidget.currentRowChanged.connect(self.stackedWidget.setCurrentIndex)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = Dialog()
    ui.show()
    sys.exit(app.exec_())

  

【微语】有趣的灵魂终会相遇, 无趣的灵魂渐行渐远 幸福也许会迟到 ,但永远不会缺席!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多