请用python,pyqt5写一个UI界面代码,具有如下功能:1.一个widget主窗口,包含5个子窗口;2.开启一个子进程,一个窗口显示mlx90640动态色温图,一个窗口显示最大温度随时间的变化曲线图;3.开启一个子进程,一个窗口显示usb摄像头的动态画面,一个窗口显示图像中物体大小与时间的变化曲线图;4.一个窗口显示程序运行过程中的的print信息,包括主进程和子进程
时间: 2023-06-25 14:06:19 浏览: 102
以下是一个简单的示例代码,实现了上述功能:
```python
import sys
import os
import time
import subprocess
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QTextEdit
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Main Widget")
self.resize(800, 600)
self.sub_widgets = []
layout = QVBoxLayout()
for i in range(5):
sub_widget = SubWidget(i)
self.sub_widgets.append(sub_widget)
layout.addWidget(sub_widget)
self.setLayout(layout)
class SubWidget(QWidget):
def __init__(self, index):
super().__init__()
self.setWindowTitle(f"Sub Widget {index + 1}")
self.resize(400, 300)
self.process = None
layout = QVBoxLayout()
if index == 0:
# MLX90640 dynamic temperature map
self.label = QLabel()
layout.addWidget(self.label)
self.start_mlx90640()
elif index == 1:
# Maximum temperature vs time curve
self.curve_label = QLabel()
layout.addWidget(self.curve_label)
self.start_temperature_curve()
elif index == 2:
# USB camera dynamic image
self.label = QLabel()
layout.addWidget(self.label)
self.start_usb_camera()
elif index == 3:
# Object size vs time curve
self.curve_label = QLabel()
layout.addWidget(self.curve_label)
self.start_object_size_curve()
elif index == 4:
# Print information
self.text_edit = QTextEdit()
self.text_edit.setReadOnly(True)
layout.addWidget(self.text_edit)
self.setLayout(layout)
def start_mlx90640(self):
# Start a subprocess to run the mlx90640 program
self.process = subprocess.Popen(["python", "mlx90640.py"], stdout=subprocess.PIPE)
# Read the temperature map from stdout and update the label
timer = QTimer(self)
timer.timeout.connect(lambda: self.update_label(self.label, self.process.stdout))
timer.start(50)
def start_temperature_curve(self):
# Start a subprocess to run the temperature curve program
self.process = subprocess.Popen(["python", "temperature_curve.py"], stdout=subprocess.PIPE)
# Read the curve image from stdout and update the label
timer = QTimer(self)
timer.timeout.connect(lambda: self.update_label(self.curve_label, self.process.stdout))
timer.start(50)
def start_usb_camera(self):
# Start a subprocess to run the usb camera program
self.process = subprocess.Popen(["python", "usb_camera.py"], stdout=subprocess.PIPE)
# Read the camera image from stdout and update the label
timer = QTimer(self)
timer.timeout.connect(lambda: self.update_label(self.label, self.process.stdout))
timer.start(50)
def start_object_size_curve(self):
# Start a subprocess to run the object size curve program
self.process = subprocess.Popen(["python", "object_size_curve.py"], stdout=subprocess.PIPE)
# Read the curve image from stdout and update the label
timer = QTimer(self)
timer.timeout.connect(lambda: self.update_label(self.curve_label, self.process.stdout))
timer.start(50)
def update_label(self, label, stdout):
# Read one line from stdout
line = stdout.readline().decode().strip()
if not line:
# Process has ended
label.setText("Process ended.")
return
if label.width() == 0 or label.height() == 0:
# Label has not been shown yet, wait for it to be shown
return
if label.pixmap() is None:
# Create a new pixmap
pixmap = QPixmap(label.width(), label.height())
pixmap.fill(Qt.white)
label.setPixmap(pixmap)
# Draw the image or curve on the pixmap
if line.startswith("image:"):
pixmap = QPixmap.fromImage(QImage.fromData(line[6:].encode()))
label.setPixmap(pixmap)
elif line.startswith("curve:"):
pixmap = QPixmap.fromImage(QImage.fromData(line[6:].encode()))
label.setPixmap(pixmap)
else:
# Append the line to the text edit
self.text_edit.append(line)
def closeEvent(self, event):
if self.process is not None and self.process.poll() is None:
# Terminate the subprocess if it is still running
self.process.terminate()
self.process.wait()
super().closeEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_widget = MainWidget()
main_widget.show()
sys.exit(app.exec_())
```
说明:
- 程序开启了一个 `MainWidget`,包含了 5 个子窗口 `SubWidget`。
- 对于每个子窗口 `SubWidget`,程序开启了一个子进程来运行对应的程序,并通过定时器和标签控件来显示程序输出的图片或曲线。
- 当 `SubWidget` 被关闭时,程序会终止对应的子进程。
阅读全文