码迷,mamicode.com
首页 > 其他好文 > 详细

【PyQt5】使用pyqtgraph绘图时UI卡顿的解决

时间:2019-08-10 10:12:05      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:比较   val   list   解决   nal   self   tsig   win   lis   

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

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

 

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_())

 

【PyQt5】使用pyqtgraph绘图时UI卡顿的解决

标签:比较   val   list   解决   nal   self   tsig   win   lis   

原文地址:https://www.cnblogs.com/zxingwork/p/11330488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!