2016-03-07 45 views
1

使用PyQt的或pyside的實時數據採集我實現利用pyqtgraph這裏https://github.com/skycaptain/gazetrack/blob/master/gui/pyqtgraph/examples/scrollingPlots.py功能啓動和使用pyqtgraph

import pyqtgraph as pg 
from pyqtgraph.Qt import QtCore, QtGui 
import numpy as np 

win = pg.GraphicsWindow() 
win.setWindowTitle('pyqtgraph example: Scrolling Plots') 
win.nextRow() 
p3 = win.addPlot() 
p4 = win.addPlot() 
# Use automatic downsampling and clipping to reduce the drawing load 
p3.setDownsampling(mode='peak') 
p4.setDownsampling(mode='peak') 
p3.setClipToView(True) 
p4.setClipToView(True) 
p3.setRange(xRange=[-100, 0]) 
p3.setLimits(xMax=0) 
curve3 = p3.plot() 
curve4 = p4.plot() 

data3 = np.empty(100) 
ptr3 = 0 

def update2(): 
    global data3, ptr3 
    data3[ptr3] = np.random.normal() 
    ptr3 += 1 
    if ptr3 >= data3.shape[0]: 
     tmp = data3 
     data3 = np.empty(data3.shape[0] * 2) 
     data3[:tmp.shape[0]] = tmp 
    curve3.setData(data3[:ptr3]) 
    curve3.setPos(-ptr3, 0) 
    curve4.setData(data3[:ptr3]) 

# update all plots 

timer = pg.QtCore.QTimer() 
timer.timeout.connect(update3) 
timer.start(50) 

## Start Qt event loop unless running in interactive mode or using pyside. 
if __name__ == '__main__': 
    import sys 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
     QtGui.QApplication.instance().exec_() 

提供起初scrollingplots比如我的節目,我想用Ctrl鍵在GUI停止按鈕+ C作爲停止連續數據繪圖的信號並將獲得的數據保存到文件中。但是,退出程序的唯一方法是關閉圖形窗口。在終端中執行Ctrl + C不會執行任何操作。

因此,我想實現一個按鈕來啓動和停止(並保存數據)在程序中。

作爲Python和麪向對象編程的新手,我在網上查找了一些示例。我發現的例子專門爲GUI中的按鈕實現:

  • stackoverflow.com/questions/8762870/how-to-implement-a-simple-button-in-pyqt

  • 組。 google.com/forum/#!topic/pyqtgraph/bxvZHtb1KKg

  • www.youtube.com/watch?v=z33vwdHrAFM和桂由YouTube用戶的

無相關教程我的例子幫助我實現我想要的,因爲我不知道如何將它們與滾動條示例結合起來。

從Qt的碰撞過程中的網頁(pyqtgraph.org/documentation/qtcrashcourse.html):

from PyQt4 import QtGui # (the example applies equally well to PySide) 
import pyqtgraph as pg 

## Always start by initializing Qt (only once per application) 
app = QtGui.QApplication([]) 

## Define a top-level widget to hold everything 
w = QtGui.QWidget() 

## Create some widgets to be placed inside 
btn = QtGui.QPushButton('press me') 
text = QtGui.QLineEdit('enter text') 
listw = QtGui.QListWidget() 
plot = pg.PlotWidget() 

## Create a grid layout to manage the widgets size and position 
layout = QtGui.QGridLayout() 
w.setLayout(layout) 

## Add widgets to the layout in their proper positions 
layout.addWidget(btn, 0, 0) # button goes in upper-left 
layout.addWidget(text, 1, 0) # text edit goes in middle-left 
layout.addWidget(listw, 2, 0) # list widget goes in bottom-left 
layout.addWidget(plot, 0, 1, 3, 1) # plot goes on right side, spanning 3 rows 

## Display the widget as a new window 
w.show() 

## Start the Qt event loop 
app.exec_() 

由於會有app.exec_()在大多數的按鈕示例代碼端和有也和更新循環的滾動圖示例本身,我很困惑,他們如何可以在同一時間運行。

我已經讀過關於如何使用gui進行連續運行的過程,我應該考慮使用計時器或多線程。不過,我目前對線程沒有任何認識。

我甚至試過Tkinter的,因爲我發現了一些關於如何使用Tkinter的與matplotlib指南 - > pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/

期待收到有關這個問題的任何建議。

+0

請添加堆棧跟蹤和代碼。您也可以查看[如何提問](http://stackoverflow.com/help/how-to-ask)來改進問題。這個問題到底在哪裏?您也可以查看[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)以改進問題。歡迎來到SO! –

+0

謝謝你的迴應。我添加了我在帖子中提到的代碼。問題是如何使用連續的實時數據採集pyqtgraph代碼和PyQt4小部件來實現多線程。 – jueeem

回答

1
from PyQt4 import QtCore, QtGui 
import pyqtgraph as pg 

class MainForm(QtGui.QMainWindow): 
    def __init__(self): 
     super(MainForm, self).__init__() 
     self.playTimer = QtCore.QTimer() 
     self.playTimer.setInterval(500) 
     self.playTimer.timeout.connect(self.playTick) 
     self.toolbar = self.addToolBar("Play") 
     self.playScansAction = QtGui.QAction(QtGui.QIcon("control_play_blue.png"), "play scans", self) 
     self.playScansAction.triggered.connect(self.playScansPressed) 
     self.playScansAction.setCheckable(True) 
     self.toolbar.addAction(self.playScansAction) 

    def playScansPressed(self): 
     if self.playScansAction.isChecked(): 
      self.playTimer.start() 
     else: 
      self.playTimer.stop() 

    def playTick(self): 
     pass 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    form = MainForm() 
    form.initUI("Scan Log Display") 
    form.show() 
    app.exec_() 

if __name__ == "__main__": 
    main()