[英]Run Real-time pyqtgraph in PlotWidget GUI
I need some help with my GUI i made with PySide and Qt Designer. 我需要用PySide和Qt Designer制作的GUI的帮助。 In my Gui code i have PlotWidget where i want to implement the pyqtgraph.
在我的Gui代码中,我具有要在其中实现pyqtgraph的PlotWidget。
The code for PyQtGraph i am trying to implement (some example i found in internet:) graph.py : 我正在尝试实现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_()
This is running without command line> python> import graph.py due to pg.QtGui.QApplication.instance().exec_()
line 由于
pg.QtGui.QApplication.instance().exec_()
行,因此没有命令行> python> import graph.py而运行
Now i want to run this random plot in Plot Widget in my GUI. 现在,我想在GUI的Plot Widget中运行此随机图。
Code From QtDesigner: 来自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
And now My main programm: 现在我的主程序是:
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_()
I have error QCoreApplication::exec: The event loop is already running
我有错误
QCoreApplication::exec: The event loop is already running
Partly i understand the problem, Pyqtgraph is made with PyQt itself and when i ask it to run the event (update the realtime graph) it says the event already working from the main gui... 我部分理解问题,Pyqtgraph由PyQt本身构成,当我要求它运行事件(更新实时图)时,它说该事件已经在主GUI中起作用了。
Please suggest how can i implement running pyqtgraph to my gui: 请建议我如何在gui中实现运行pyqtgraph:
appreciate for modified code: 感谢修改后的代码:
Thanks you very much! 非常感谢你!
These changes to your function are required: all about keeping hold of those variables. 需要对函数进行这些更改:所有与保持这些变量有关。 On my system I had specific errors saying that variables were undefined to aid with this.
在我的系统上,我遇到了一些具体的错误,即未定义变量来帮助实现这一点。
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.