PyQt使用笔记(五) cgitb提示异常 pyqtgraph绘图库 2021.03.18

1. PyQt5的异常无法显示, 一直很头痛,今天查阅资料, 发现使用cgitb可以正常打印异常, 在主文件中添加如下代码即可.

import cgitb
cgitb.enable()

2. 闲逛github, 发现一个基于PyQt(Pyside)的超酷炫的绘图库pyqtgraph, 其组件基本都是继承自PyQt的QObject, 能够直接当作PyQt的组件使用.

首先创建widget, 和PyQt类似:

#pyqtgraph_first


import numpy as np
import pyqtgraph as pg



class MyGraphicWidget(pg.GraphicsLayoutWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resize(500, 300)
        self.setWindowTitle('pyqtgraph-example')
        self.addgraph()

    def addgraph(self):
        self.plot1 = self.addPlot(title='first plotting', y=np.random.normal(size=100))

        x1 = np.random.normal(size=100)
        x2 = np.random.normal(size=110) + 5
        x3 = np.random.normal(size=120) + 10

        self.plot2 = self.addPlot(title='second plotting')
        self.plot2.plot(x1, pen=[255, 0, 0], name='red')
        self.plot2.plot(x2, pen=[0, 255, 0], name='green')
        self.plot2.plot(x3, pen=[0, 0, 255], name='blue')

 然后将以上组件当作PyQt组件引入,并布局:

from pyqtgraph_first import MyGraphicWidget
from PyQt5.QtCore import Qt
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
import cgitb

cgitb.enable()

class Ui_MainWindow:
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("Form")
        MainWindow.showMaximized()
        self.graph = MyGraphicWidget(parent=MainWindow, show=True, title='plot!')
        self.graph.resize(800, 500)
        self.graph.setBackground('k')
        self.graph.setGeometry(QtCore.QRect(105, 105, 1400, 700))


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    MainWindow =QMainWindow()
    ui_window = Ui_MainWindow()
    ui_window.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

执行后得到的图像如下, 在图像上, 可以通过鼠标进行坐标轴的拖动, 曲线的缩小放大, 保存导出等功能.

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天地之心online

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值