1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton
from PyQt5.QtCore import QTimer, QThread, QEventLoop, QThread
import pyqtgraph as pg
import sys
import time
import math
import numpy as np
class test(QThread):
def __init__(self, ui):
QThread.__init__(self)
print("Thread initialisé")
self.ui=ui
self.i = 0
self.x=[]
self.y=[]
self.y2=[]
self.y3=[]
self.data_Timer = QTimer()
self.data_Timer.moveToThread(self)
self.data_Timer.timeout.connect(self.collect_Data)
def run(self):
print("Thread lancé!")
self.data_Timer.start(250)
loop = QEventLoop()
loop.exec_()
def collect_Data(self):
self.i = self.i + 1
self.x.append(self.i)
self.y.append(math.cos(self.i))
self.y2.append(math.sin(self.i))
if math.cos(self.i) > 0.75:
self.y3.append(math.cos(self.i))
else:
self.y3.append(np.nan)
self.ui.graphique_1.setData(self.x, self.y)
self.ui.graphique_2.setData(self.x, self.y2)
self.ui.graphique_3.setData(self.x, self.y3)
def stop(self):
self.quit()
print("thread stoppé")
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.fenetre_widget = QWidget()
self.graphique_live = pg.GraphicsWindow()
self.plot1 = self.graphique_live.addPlot(0, 0, title="azerty")
self.graphique_1 = self.plot1.plot([], [], pen=pg.mkPen('w', width=1))
self.graphique_2 = self.plot1.plot([], [], pen=pg.mkPen('g', width=1))
self.graphique_3 = self.plot1.plot([], [], pen=None, symbol='o', symbolBrush =(0, 255, 255), symbolSize = 14, symbolPen ='w', connect="finite")
self.bouton_go = QPushButton("GO")
self.bouton_stop = QPushButton("Stop")
layout_vertical = QVBoxLayout()
layout_vertical.addWidget(self.graphique_live)
layout_vertical.addWidget(self.bouton_go)
layout_vertical.addWidget(self.bouton_stop)
self.thread_get_data = test(self)
self.bouton_go.clicked.connect(self.tracer)
self.bouton_stop.clicked.connect(self.stop)
self.fenetre_widget.setLayout(layout_vertical)
self.setCentralWidget(self.fenetre_widget)
def stop(self):
self.thread_get_data.stop()
def tracer(self):
self.thread_get_data.start()
if __name__ == "__main__":
appli = QApplication(sys.argv)
fenetre_main = MainWindow()
fenetre_main.show()
sys.exit(appli.exec_()) |
Partager