티스토리 뷰
python/GUI
(macOS)[python][PySide6] Real-Time Embedded GUI System Simulator with Qt Designer
jinozpersona 2022. 11. 25. 22:50Intro
OS : MacOS, Ventura, v13.0.1
SW : Visual Studio Code, Qt Designer
python : 3.10.8
Arduino 신호를 임의로 받는 시뮬레이터를 만들고 Qt Designer로 system GUI 제작후 실행하는 python coding
Requirements
- vscode extension : Qt for Python
- python package : pyside6
- Qt Designer
pyside6 install
$ pip3 install PyQt6
$ pip3 install PySide6
$ pip3 install matplotlib --upgrade
Qt designer
$ open -a designer
# 참고: zsh alias
$ nano ~/.zshrc
....
alias designer='open -a designer'
$ designer
Version 확인 : 6.4.1
import PyQt6.QtCore
print(PyQt6.QtCore.qVersion())
1. Signal Generator : Arduino Temp. sensor 2개 신호를 받는다고 가정
personafun/signal_gen.py
import numpy as np
import time
def ard_signal(sampleTime):
sd = np.random.rand(2)
time.sleep(sampleTime)
ard = ''.join('%.2f%.2f' % (round(sd[0],2)*2 + 20,round(sd[1],2)*3 + 25))
return ard
2. GUI : Qt Designer UI 생성
[Your-Project-Folder]$ designer
MainWindow > 생성
1. Quit Push button
Button : QUIT
objectName : quitButton
2. just Label
Label : Sensor#1
Label : Sensor#2
3. Sensor Value 표시
LCD Number 2개
objectName : lcdNumber_S1
objectName : lcdNumber_S2
4. Plot : Layouts > Vertical Layout
objectName : graph_verticalLayout
저장 : [Project 폴더] personaDaqGui.ui
3. python Coding
[Your-Project-Folder]$ personaDaq.py
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer, QDateTime
from PySide6.QtUiTools import QUiLoader, loadUiType
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
from personafun.signal_gen import ard_signal
class MplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi, facecolor='gray')
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
## Ui connect
uiclass, baseclass = loadUiType('personaDaqGui.ui')
class PersonaWindow(uiclass, baseclass):
def __init__(self, *args, **kwargs):
super(PersonaWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.setWindowTitle('Persona DAQ')
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
self.graph_verticalLayout.addWidget(self.canvas)
self.update_plot()
## Setup a timer to trigger the redraw by calling update_plot.
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
## Button connect
self.quitButton.clicked.connect(self.close)
def update_plot(self):
global x_sum, y_sum, sampleTime
## Ard Signal Generator
ard = ard_signal(sampleTime)
datas = ard
T1S1 = float(datas[0:5])
T2S2 = float(datas[5:10])
y_new = np.array([[T1S1,T2S2]])
## y_new, y_sum
y_sum = np.append(y_sum,y_new,axis=0)
x_sum.append(len(y_sum)*sampleTime)
## lcd set
self.lcdNumber_S1.display(T1S1)
self.lcdNumber_S1.setSmallDecimalPoint(True)
self.lcdNumber_S1.setDigitCount(5)
self.lcdNumber_S2.display(T2S2)
self.lcdNumber_S2.setSmallDecimalPoint(True)
self.lcdNumber_S2.setDigitCount(5)
## Clear the canvas & Set-up axes
self.canvas.axes.cla()
self.canvas.axes.set_facecolor("black")
self.canvas.axes.plot(x_sum, y_sum[:,0],'ro-',label='Temp.1')
self.canvas.axes.plot(x_sum, y_sum[:,1],'go-',label='Temp.2')
self.canvas.axes.set_xlim([int(0),int(x_sum[-1]+sampleTime*2)])
self.canvas.axes.set_ylim([10.0,50.0])
self.canvas.axes.set_xticks(range(0,x_sum[-1],sampleTime))
self.canvas.axes.xaxis.set_tick_params(color='white',labelcolor='white')
self.canvas.axes.yaxis.set_tick_params(color='white',labelcolor='white')
self.canvas.axes.set_title('Temp. vs Time',color='white')
self.canvas.axes.set_xlabel('Time[sec]',color='white')
self.canvas.axes.set_ylabel('Temp.[oC]',color='white')
self.canvas.axes.legend(loc="upper left")
self.canvas.axes.grid()
self.canvas.draw()
#################### Code start ####################
## initial variable : global
sampleTime = 1
x_sum = []
y_sum = np.empty((0,2), dtype=float)
#################### Code end ####################
if __name__=="__main__":
app = QApplication(sys.argv)
window = PersonaWindow()
window.show()
app.exec()
실행화면 : QUIT 누르면 종료
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 코로나19
- 코로나
- Pandas
- Python
- 자가격리
- ERP
- 확진
- Django
- Raspberry Pi
- template
- DAQ
- server
- Regression
- CSV
- DS18B20
- SSH
- Templates
- analysis
- raspberrypi
- r
- vscode
- MacOS
- Model
- github
- pyserial
- sublime text
- arduino
- 라즈베리파이
- COVID-19
- git
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함