【问题标题】:Why this simple example of pyqtgraph is not working?为什么这个简单的 pyqtgraph 示例不起作用?
【发布时间】:2017-11-25 06:57:06
【问题描述】:

以下代码仅用于测试 pyqtgraph 的速度。我所期望的是永远得到交替图。但是,执行此代码后,小部件中不会显示任何内容。有什么问题?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from random import randint, uniform
from math import *
import pyqtgraph as pg
import time

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.x=pg.PlotWidget(self)
        self.x.setMinimumHeight(400)
        self.x.setMinimumWidth(400)
        self.setWindowState(Qt.WindowMaximized)
        self.u=[i+uniform(1,30) for i in range(1000)]
        self.v=[-i+uniform(1,30) for i in range(1000)]
        self.show()

    def Run(self):
        while 1:
            self.x.clear()
            self.x.plot(self.u)
            self.x.clear()
            self.x.plot(self.v)

app=QApplication(sys.argv)
ex=Example()
ex.Run()
sys.exit(app.exec_())

【问题讨论】:

    标签: python multithreading pyqt pyqtgraph


    【解决方案1】:

    在 GUI 中使用 while 循环通常是个坏主意。问题在于它阻止了 GUI 保持响应并处理所有 GUI 事件。

    一个选项是使用计时器,例如一个简单的QTimer。为了在两个不同的数据集之间切换以进行绘图,您还需要引入一些机制来确定应该显示哪个数据集。

    import sys
    #from PyQt5.QtWidgets import *
    #from PyQt5.QtCore import *
    from PyQt4 import QtGui, QtCore
    from random import randint, uniform
    import pyqtgraph as pg
    
    class Example(QtGui.QWidget):
    
        def __init__(self):
            QtGui.QWidget.__init__(self)
            self.x=pg.PlotWidget(self)
            self.x.setMinimumHeight(400)
            self.x.setMinimumWidth(400)
            self.setWindowState(QtCore.Qt.WindowMaximized)
            self.u=[i+uniform(1,30) for i in range(1000)]
            self.v=[-i+uniform(1,30) for i in range(1000)]
            self.switch = True
            self.show()
    
        def start(self):
            self.timer = QtCore.QTimer(self)
            self.timer.timeout.connect(self.run)
            self.timer.start(500)
    
        def run(self):
            if self.switch:
                self.x.clear()
                self.x.plot(self.u)
            else:
                self.x.clear()
                self.x.plot(self.v)
            self.switch = not self.switch
    
    app=QtGui.QApplication(sys.argv)
    ex=Example()
    ex.start()
    sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2011-04-09
      • 2012-08-17
      • 2011-10-25
      相关资源
      最近更新 更多