zoukankan      html  css  js  c++  java
  • 【PyQt5】使用pyqtgraph绘图时UI卡顿的解决

    思路是把所有数据处理计算的过程放在主线程之外的子线程内

    问题,如果不对数据进行剪裁,长时间过后主线程内的绘图会消耗比较多的时间

    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5 import QtWidgets,QtGui,QtCore
    import pyqtgraph as pg
    import sys,os,random,time,psutil
    
    class UI_MainWindow(QMainWindow):
        def __init__(self):
            super(UI_MainWindow, self).__init__()
            self.setWindowTitle('CPUinfo')
            self.setWindowIcon(QIcon('./CPU.png'))
            self.resize(600,480)
    
            self.main_widget = QWidget()
            self.main_layout = QGridLayout()
            self.main_widget.setLayout(self.main_layout)
            self.setCentralWidget(self.main_widget)
            self.plot_widget = QWidget()
            self.plot_layout = QGridLayout()
            self.plot_widget.setLayout(self.plot_layout)
    
            self.plot_plt = pg.PlotWidget()
            self.plot_plt.setYRange(max=100,min=0)
            self.plot_layout.addWidget(self.plot_plt)
            self.plot_plt.setYRange(max=100,min=0)
    
            self.main_layout.addWidget(self.plot_widget)
    
            self.setCentralWidget(self.main_widget)
    
    class NewThread(QThread):
        trigger = pyqtSignal(list)
        def __init__(self):
            super(NewThread, self).__init__()
            self.dataList = []
    
        def run(self):
            print('NewThread start!')
            timelist = []
            while True:
                time.sleep(1)
                # Xtime = time.strftime("%H%M%S")
                Ycpu = "%0.2f" % psutil.cpu_percent(interval=1)
                self.dataList.append(float(Ycpu))
                # timelist.append(float(Xtime))
                # print(Xtime, Ycpu)
                self.trigger.emit(self.dataList)
    
    
    class showMainWindow(UI_MainWindow):
        def __init__(self):
            super(showMainWindow, self).__init__()
            self.Thread1 = NewThread()
            self.Thread1.trigger.connect(self.Plot)
    
            self.Thread1.start()
    
        def Plot(self,list1):
            self.plot_plt.plot().setData(list1)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        gui = showMainWindow()
        gui.show()
        sys.exit(app.exec_())

     更新:解决一次时间长了之后一次绘制太多点导致卡顿问题

    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5 import QtWidgets,QtGui,QtCore
    import pyqtgraph as pg
    import sys,os,random,time,psutil
    # from UI import UI_MainWindow
    
    class UI_MainWindow(QMainWindow):
        def __init__(self):
            super(UI_MainWindow, self).__init__()
            self.setWindowTitle('CPUinfo')
            self.setWindowIcon(QIcon('./CPU.png'))
            self.resize(600,480)
    
            self.main_widget = QWidget()
            self.main_layout = QGridLayout()
            self.main_widget.setLayout(self.main_layout)
            self.setCentralWidget(self.main_widget)
            self.plot_widget = QWidget()
            self.plot_layout = QGridLayout()
            self.plot_widget.setLayout(self.plot_layout)
    
            self.plot_plt = pg.PlotWidget()
            self.plot_plt.setYRange(max=100,min=0)
            self.plot_layout.addWidget(self.plot_plt)
            self.plot_plt.setYRange(max=100,min=0)
    
            self.main_layout.addWidget(self.plot_widget)
    
            self.setCentralWidget(self.main_widget)
    
            
    class NewThread(QThread):
        trigger = pyqtSignal(list,list)
        def __init__(self):
            super(NewThread, self).__init__()
            self.dataList = []
            self.timeList = []
            self.a = 0
    
        def run(self):
            print('NewThread start!')
    
            while True:
                # time.sleep(1)
                # Xtime = time.strftime("%H%M%S")
                Ycpu = "%0.2f" % psutil.cpu_percent(interval=1)
                self.dataList.append(float(Ycpu))
                self.timeList.append(self.a)
                # timelist.append(float(Xtime))
                # print(Xtime, Ycpu)
                self.a += 1
                self.trigger.emit(self.dataList,self.timeList)
    
    
    
    class showMainWindow(UI_MainWindow):
        def __init__(self):
            super(showMainWindow, self).__init__()
            self.Thread1 = NewThread()
            self.Thread1.trigger.connect(self.Plot)
    
            self.Thread1.start()
    
    
    
        def Plot(self,list,list1):
            self.plot_plt.plot().setData(list1[len(list1)-2:],list[len(list)-2:],pen='g',symbol='o')
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        gui = showMainWindow()
        gui.show()
        sys.exit(app.exec_())
  • 相关阅读:
    HTML Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用
    CSS+DIV定位分析(relative,absolute,static,fixed)
    Web中常用字体介绍
    CSS中强大的EM
    一线开发忙着实现,二线开发忙着变现
    Eclipse之父、《设计模式》作者、Junit作者之Erich Gamma
    著名软件工程师与作家、极限编程的创始者、JUnit作者之Kent Beck
    学习要构造反馈闭环
    技术人员也要全面发展
    2019第13周日
  • 原文地址:https://www.cnblogs.com/zxingwork/p/11330488.html
Copyright © 2011-2022 走看看