QApplication 关闭窗口 进程还在运行
时间: 2023-07-14 16:13:12 浏览: 133
当你关闭一个Qt窗口时,QApplication并不会自动退出。这意味着你需要手动调用QApplication的quit()函数来终止应用程序。例如,在你的窗口的closeEvent()事件处理程序中添加以下代码:
```python
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message', "Are you sure you want to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
QApplication.quit()
else:
event.ignore()
```
当用户点击关闭按钮时,会显示一个消息框,询问用户是否真的要退出。如果用户确认退出,则调用QApplication的quit()函数来终止应用程序。
相关问题
请用python,pyqt5写一个UI界面代码,具有如下功能:1.一个widget主窗口,包含5个子窗口;2.开启一个子进程,一个窗口显示mlx90640动态色温图,一个窗口显示最大温度随时间的变化曲线图;3.开启一个子进程,一个窗口显示usb摄像头的动态画面,一个窗口显示图像中物体大小与时间的变化曲线图;4.一个窗口显示程序运行过程中的的print信息,包括主进程和子进程
以下是一个简单的示例代码,实现了上述功能:
```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` 被关闭时,程序会终止对应的子进程。
pyqt5如何实现点击窗体关闭图标进程仍在
在PyQt5中,当用户点击窗口的关闭图标(通常表现为X形状)时,默认的行为是关闭整个应用程序或窗口。如果你想让窗口关闭但保持进程运行,可以在槽函数`closeEvent()`中处理这个情况。这里是一个简单的例子:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
class MyWindow(QMainWindow):
def closeEvent(self, event): # 捕获关闭事件
print("窗口即将关闭...")
# 如果你需要做清理工作或者保存数据,可以在这里操作
# 然后阻止事件的默认处理(关闭窗口)
event.ignore() # 设置event.accept()将正常关闭窗口
# 或者设置event.setAccepted(False)同样阻止关闭
self.showMinimized() # 替换为showNormal()或其他的最小化操作
if __name__ == "__main__":
app = QApplication([])
window = MyWindow()
window.setWindowTitle("我的窗口")
window.show()
app.exec_()
```
在这个例子中,当你点击关闭按钮时,会打印一条消息并进入最小化状态而不是完全关闭。你可以在此基础上根据需要添加额外的操作。
阅读全文