Skip to content

Instantly share code, notes, and snippets.

@tianrking
Last active August 7, 2023 09:01
Show Gist options
  • Save tianrking/63a0dbe6410a6ef9c854ebfabf5fb29e to your computer and use it in GitHub Desktop.
Save tianrking/63a0dbe6410a6ef9c854ebfabf5fb29e to your computer and use it in GitHub Desktop.
Pyqtgraph & Pyside6
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QPushButton
from PySide6.QtCore import QThread, Signal
import pyqtgraph as pg
import numpy as np
import random
import time
class SerialThread(QThread):
data1_received = Signal(float)
data2_received = Signal(float)
def run(self):
while True:
data1 = random.uniform(0, 100)
data2 = random.uniform(-5, 20)
self.data1_received.emit(data1)
self.data2_received.emit(data2)
time.sleep(0.1)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('w0x7ce')
self.data1 = np.array([])
self.data2 = np.array([])
self.max_data_length = 1000
self.total_data_count = 0
self.graphWidget = pg.PlotWidget()
self.graphWidget.setBackground('w')
self.plot1 = self.graphWidget.plot(pen='r')
self.plot2 = self.graphWidget.plot(pen='b')
self.data_count_label = QLabel()
# Create buttons
self.is_plotting = True
self.start_button = QPushButton("Start")
self.start_button.clicked.connect(self.start_plotting)
self.stop_button = QPushButton("Stop")
self.stop_button.clicked.connect(self.stop_plotting)
self.plot_button = QPushButton("Start")
self.plot_button.clicked.connect(self.toggle_plotting)
layout = QVBoxLayout()
layout.addWidget(self.graphWidget)
layout.addWidget(self.data_count_label)
layout.addWidget(self.plot_button)
# layout.addWidget(self.start_button)
# layout.addWidget(self.stop_button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.serial_thread = SerialThread()
self.serial_thread.data1_received.connect(self.update_plot1)
self.serial_thread.data2_received.connect(self.update_plot2)
self.serial_thread.start()
def toggle_plotting(self):
if self.is_plotting:
self.stop_plotting()
self.is_plotting = False
self.plot_button.setText("Start")
else:
self.start_plotting()
self.is_plotting = True
self.plot_button.setText("Stop")
def update_plot1(self, data):
self.data1 = np.append(self.data1, data)
self.total_data_count += 1
if len(self.data1) > self.max_data_length:
self.data1 = self.data1[-self.max_data_length:]
self.plot1.setData(self.data1)
def update_plot2(self, data):
self.data2 = np.append(self.data2, data)
if len(self.data2) > self.max_data_length:
self.data2 = self.data2[-self.max_data_length:]
self.plot2.setData(self.data2)
self.data_count_label.setText(f"Data count: {self.total_data_count}")
def start_plotting(self):
self.serial_thread.start()
def stop_plotting(self):
self.serial_thread.terminate()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment