我正在建立一个实时数据采集绘图控制系统。我有一个用pyqt4编写的GUI,有3个控制按钮,一个quit按钮和嵌入的matplotlib图,我应该显示实时数据。数据正在打印到终端屏幕,但从未打印到GUI中。如果我关闭UI,图形将出现,但什么也不显示。plot函数也可以单独工作,只需使用血小板()图=plt.图()[定义ax前两者]ax=图add\ U子批次(111)和变化自拔()至图画布绘制()
我试过使用pyqt4和pyqt5使用不同的方法。我认为它对展示什么感到困惑。我为GUI设置了主窗口和三个按钮,单独工作,但是如果包含绘图,就不能与它们交互
'''
import sys
from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
FigureCanvas
import time
import numpy as np
from matplotlib.figure import Figure
import explorerhat
'''
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(400, 400, 700, 500)
self.setWindowTitle("PiDAQ")
## Define Canvas size and location to be embedded into Main Window
canvas = Canvas(self, width = 4, height = 4)
canvas.move(200,20)
self.show()
''' END CLASS'''
class Canvas(FigureCanvas):
def __init__(self, parent = None, width =5 , height = 5, dpi =100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.plot()
'''
def plot(self):
t0 = time.time()
i=0
print('t0=', t0)
ax = self.figure.add_subplot(111)
N=50
x = np.arange(N)*20/N
y1 = np.arange(N)*5/N
y2 = np.arange(N)*5/N
line1, = self.ax.plot(x, y1, 'b-')
line2, = self.ax.plot(x, y2, 'r-')
while True:
i =+ 1
V2 = self.explorerhat.analog.one.read()
V3 = self.explorerhat.analog.two.read()
V4 = self.explorerhat.analog.three.read()
V5 = self.explorerhat.analog.four.read()
if V2 > 2.5:
output = ' POW!!! '
else:
output = ''
t = time.time()-t0
if i % N == 0:
print(i, t, V2, V3, V4, V5, output)
x[i%N-1] = t
if i % N == 0:
y1[-1] = V2
y2[-1] = V3
line1.set_xdata(x-x[0])
line1.set_ydata(y1)
line2.set_xdata(x-x[0])
line2.set_ydata(y2)
self.draw()
else:
y1[i%N-1] = V2
y2[i%N-1] = V3
time.sleep(0.00001)
'''
问题是plot函数永远不会返回,因此会阻塞事件循环。你知道吗
Qt的工作原理是让一个无限循环(事件循环)侦听事件,然后将它们分配给偶数个处理程序。事件可以是键盘按下、鼠标单击、重绘请求、TCP通知等。事件处理程序是处理该事件的函数。它们应该很快返回,因为只要它们在运行,Qt就不能处理其他事件(Qt只有一个线程可以处理GUI事件)。不返回的事件处理程序使应用程序“挂起”。有关更详细的解释,请参见this artcicle。你知道吗
由于发生了某些事件(例如,按钮单击事件处理程序发出连接到插槽的单击信号),因此会直接或间接调用所有方法。因此,您编写的所有方法都应该快速返回(例如,一秒钟内返回)。
plot
函数不返回,因此程序挂起。你知道吗解决方案是从plot函数中删除while循环,并将内容放在QTimer定期调用的单独方法中。像这样的。你知道吗
免责声明,我没有运行上面的代码。它可能包含小错误。你知道吗
相关问题 更多 >
编程相关推荐