![](/img/trans.png)
[英]PyQtGraph PlotWidget : how to force each draw (changing range) for "real time" plotting
[英]Run Real-time pyqtgraph in PlotWidget GUI
我需要用PySide和Qt Designer制作的GUI的幫助。 在我的Gui代碼中,我具有要在其中實現pyqtgraph的PlotWidget。
我正在嘗試實現PyQtGraph的代碼(我在互聯網上找到了一些示例:) graph.py:
import pyqtgraph as pg
import numpy as np
plt = pg.plot()
bufferSize = 1000
data = np.zeros(bufferSize)
curve = plt.plot()
line = plt.addLine(x=0)
plt.setRange(xRange=[0, bufferSize], yRange=[-50, 50])
i = 0
def update():
global data, curve, line, i
n = 10 # update 10 samples per iteration
rand = np.random.normal(size=n)
data[i:i+n] = np.clip(data[i-1] + rand, -50, 50)
curve.setData(data)
i = (i+n) % bufferSize
line.setValue(i)
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(20)
pg.QtGui.QApplication.instance().exec_()
由於pg.QtGui.QApplication.instance().exec_()
行,因此沒有命令行> python> import graph.py而運行
現在,我想在GUI的Plot Widget中運行此隨機圖。
來自QtDesigner的代碼:
from PySide import QtCore, QtGui
class Ui_MainDialog(object):
def setupUi(self, MainDialog):
MainDialog.setObjectName("MainDialog")
MainDialog.resize(400, 300)
self.nameEdit = QtGui.QLineEdit(MainDialog)
self.nameEdit.setGeometry(QtCore.QRect(30, 50, 181, 31))
self.nameEdit.setText("")
self.nameEdit.setObjectName("nameEdit")
self.showButton = QtGui.QPushButton(MainDialog)
self.showButton.setGeometry(QtCore.QRect(250, 50, 101, 31))
self.showButton.setObjectName("showButton")
self.MainGraph = PlotWidget(MainDialog)
self.MainGraph.setGeometry(QtCore.QRect(70, 100, 256, 192))
self.MainGraph.setObjectName("MainGraph")
self.retranslateUi(MainDialog)
QtCore.QMetaObject.connectSlotsByName(MainDialog)
MainDialog.setTabOrder(self.showButton, self.nameEdit)
MainDialog.setTabOrder(self.nameEdit, self.MainGraph)
def retranslateUi(self, MainDialog):
MainDialog.setWindowTitle(QtGui.QApplication.translate("MainDialog", "MainDialog", None, QtGui.QApplication.UnicodeUTF8))
self.nameEdit.setPlaceholderText(QtGui.QApplication.translate("MainDialog", "Log Window", None, QtGui.QApplication.UnicodeUTF8))
self.showButton.setText(QtGui.QApplication.translate("MainDialog", "Show Graph!", None, QtGui.QApplication.UnicodeUTF8))
from pyqtgraph import PlotWidget
現在我的主程序是:
from PySide.QtCore import *
from PySide.QtGui import *
import example3
import sys
class Maindialog(QDialog,example3.Ui_MainDialog):
def __init__(self, parent=None):
super(Maindialog,self).__init__(parent)
self.setupUi(self)
self.connect(self.showButton,SIGNAL("clicked()"),self.showBOX)
def showBOX(self):
QMessageBox.information(self, "hello", "hello there " + self.nameEdit.text())
print"hello world"
plt=self.MainGraph
#z.plot(x = [0, 1, 2, 4,7,8,9,0], y = [4, 5, 9, 6,1,2,3,4])
import numpy as np
import pyqtgraph as pg
bufferSize = 1000
data = np.zeros(bufferSize)
curve = plt.plot()
curve.setData()
line = plt.addLine(x=0)
plt.setRange(xRange=[0, bufferSize], yRange=[-50, 50])
i = 0
def update():
global data, curve, line, i
n = 10 # update 10 samples per iteration
rand = np.random.normal(size=n)
data[i:i+n] = np.clip(data[i-1] + rand, -50, 50)
curve.setData(data)
i = (i+n) % bufferSize
line.setValue(i)
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(20)
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.instance().exec_()
我有錯誤QCoreApplication::exec: The event loop is already running
我部分理解問題,Pyqtgraph由PyQt本身構成,當我要求它運行事件(更新實時圖)時,它說該事件已經在主GUI中起作用了。
請建議我如何在gui中實現運行pyqtgraph:
感謝修改后的代碼:
非常感謝你!
需要對函數進行這些更改:所有與保持這些變量有關。 在我的系統上,我遇到了一些具體的錯誤,即未定義變量來幫助實現這一點。
def showBOX(self):
QMessageBox.information(self, "hello", "hello there " + self.nameEdit.text())
print"hello world"
plt=self.MainGraph
#z.plot(x = [0, 1, 2, 4,7,8,9,0], y = [4, 5, 9, 6,1,2,3,4])
import numpy as np
import pyqtgraph as pg
bufferSize = 1000
self.data = np.zeros(bufferSize)
self.curve = plt.plot()
self.curve.setData()
self.line = plt.addLine(x=0)
plt.setRange(xRange=[0, bufferSize], yRange=[-50, 50])
self.i = 0
def update():
n = 10 # update 10 samples per iteration
rand = np.random.normal(size=n)
self.data[self.i:self.i+n] = np.clip(self.data[self.i-1] + rand, -50, 50)
self.curve.setData(self.data)
self.i = (self.i+n) % bufferSize
self.line.setValue(self.i)
self.update = update
self.timer = pg.QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(20)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.