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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| from PyQt5.QtCore import QThread, pyqtSignal, QSize
from PyQt5.QtWidgets import QPushButton, QWidget, QMainWindow, QApplication, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QPixmap, QTextCursor, QColor, QIcon
import pyqtgraph as pg
import pandas as pd
import numpy as np
import sys
import random
import time
class update_datas_live_simulation(QThread):
signal_update_simulation = pyqtSignal()
def __init__(self, ui):
super().__init__()
print("Thread initialisé")
self.ui = ui
def run(self):
print("Thread lancé...")
self.x = []
self.y_1 = []
self.y_2 = []
self.y_3 = []
self.calcul()
def calcul(self):
for i in range(0, 50000, 1):
time.sleep(0.01)
self.data = random.random()*10
self.x.append(i)
self.y_1.append(self.data)
if self.data > 9:
self.y_2.append(self.data)
self.y_3.append(np.nan)
elif self.data < 1:
self.y_2.append(np.nan)
self.y_3.append(self.data)
else:
self.y_2.append(np.nan)
self.y_3.append(np.nan)
self.signal_update_simulation.emit()
def stop(self):
self.quit()
print("Thread stoppé")
class Bouton_simple(QPushButton):
def __init__(self, ui, longueur, hauteur, texte, taille_texte, icone):
QPushButton.__init__(self)
self.setStyleSheet("""QPushButton{font: """ + str(taille_texte) + """px}""")
if longueur!=None:
self.setFixedWidth(longueur)
if hauteur != None:
self.setFixedHeight(hauteur)
self.setText(texte)
if not icone == None:
self.setIcon(QIcon(icone[1]))
self.setIconSize(QSize(20, 20))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
#np.warnings.filterwarnings('ignore')
self.init_variables()
self.instances_widgets()
self.connect_widgets_to_functions()
self.construction_fenetre()
def init_variables(self):
pass
def instances_widgets(self):
self.fenetre_widget = QWidget()
self.bouton_go = Bouton_simple(self, longueur=None, hauteur=None, texte="GO", taille_texte=12, icone = None)
self.bouton_stop = Bouton_simple(self, longueur=None, hauteur=None, texte="STOP", taille_texte=12, icone = None)
self.graphique_live = pg.GraphicsWindow()
self.plot1 = self.graphique_live.addPlot(0, 0, title = "Courbe de test")
self.plot1.addLegend()
self.graphique_1 = self.plot1.plot([], [], pen=pg.mkPen('w', width=1))
self.graphique_2 = self.plot1.plot([], [], pen=None, symbol='o', symbolBrush =(0, 255, 0), symbolSize = 14, symbolPen ='w', connect="finite")
self.graphique_3 = self.plot1.plot([], [], pen=None, symbol='o', symbolBrush =(255, 0, 0), symbolSize = 14, symbolPen ='w', connect="finite")
self.thread_simulation = update_datas_live_simulation(self)
self.thread_simulation.finished.connect(self.terminate_thread_simulation)
self.thread_simulation.signal_update_simulation.connect(self.data_from_simulation)
def connect_widgets_to_functions(self):
self.bouton_go.clicked.connect(lambda: self.start_thread_simulation())
self.bouton_stop.clicked.connect(lambda: self.stop())
def construction_fenetre(self):
self.layout_horizontal_global = QHBoxLayout()
self.layout_vertical_1 = QVBoxLayout()
self.layout_vertical_2 = QVBoxLayout()
self.layout_horizontal_global.addLayout(self.layout_vertical_1)
self.layout_horizontal_global.addLayout(self.layout_vertical_2)
self.layout_vertical_1.addWidget(self.graphique_live)
self.layout_vertical_2.addWidget(self.bouton_go)
self.layout_vertical_2.addWidget(self.bouton_stop)
self.fenetre_widget.setLayout(self.layout_horizontal_global)
self.setCentralWidget(self.fenetre_widget)
def start_thread_simulation(self):
print("Thread Start!")
try:
self.thread_simulation.start()
except Exception as e:
print(e)
def data_from_simulation(self):
try:
self.graphique_1.setData(self.thread_simulation.x, self.thread_simulation.y_1)
self.graphique_2.setData(self.thread_simulation.x, self.thread_simulation.y_2)
self.graphique_3.setData(self.thread_simulation.x, self.thread_simulation.y_3)
except Exception as e:
print(e)
def terminate_thread_simulation(self):
print("Fin du thread")
def stop(self):
print("Demande de stopper le thread")
if self.thread_simulation.isRunning():
self.thread_simulation.stop()
else:
print("No")
if __name__ == "__main__":
appli = QApplication(sys.argv)
fenetre_main = MainWindow()
fenetre_main.show()
sys.exit(appli.exec_()) |
Partager