def chose_cam(self): self.stop() MessageBox( self.closeButton, title='Tips', text='Loading camera', time=2000, auto=True).exec_() _, cams = Camera().get_cam_num() popMenu = QMenu() popMenu.setFixedWidth(self.cameraButton.width())
时间: 2023-06-12 16:04:38 浏览: 93
这是一个 Python 类中的方法,用于选择摄像头。首先,它调用了 `stop()` 方法来停止当前正在运行的摄像头。然后,它弹出了一个消息框来提示用户正在加载摄像头。接着,它使用 `Camera()` 类来获取可用摄像头的数量并将其存储在 `cams` 变量中。最后,它创建了一个 `QMenu` 弹出菜单,并将其宽度设置为与 `cameraButton` 按钮相同。
相关问题
程序运行提示QBasicTimer::stop: Failed. Possibly trying to stop from a different thread,修改程序class MyWindow(QWidget): def init(self): super().init() self.thread_list = [] self.color_photo_dir = os.path.join(os.getcwd(), "color_photos") self.depth_photo_dir = os.path.join(os.getcwd(), "depth_photos") self.image_thread = None self.saved_color_photos = 0 # 定义 saved_color_photos 属性 self.saved_depth_photos = 0 # 定义 saved_depth_photos 属性 self.init_ui() def init_ui(self): self.ui = uic.loadUi("C:/Users/wyt/Desktop/D405界面/intelrealsense1.ui") self.open_btn = self.ui.pushButton self.color_image_chose_btn = self.ui.pushButton_3 self.depth_image_chose_btn = self.ui.pushButton_4 self.open_btn.clicked.connect(self.open) self.color_image_chose_btn.clicked.connect(lambda: self.chose_dir(self.ui.lineEdit, "color")) self.depth_image_chose_btn.clicked.connect(lambda: self.chose_dir(self.ui.lineEdit_2, "depth")) def open(self): self.profile = self.pipeline.start(self.config) self.is_camera_opened = True self.label.setText('相机已打开') self.label.setStyleSheet('color:green') self.open_btn.setEnabled(False) self.close_btn.setEnabled(True) self.image_thread = ImageThread(self.pipeline, self.color_label, self.depth_label, self.interval, self.color_photo_dir, self.depth_photo_dir, self._dgl) self.image_thread.saved_color_photos_signal.connect(self.update_saved_color_photos_label) self.image_thread.saved_depth_photos_signal.connect(self.update_saved_depth_photos_label) self.image_thread.start() def chose_dir(self, line_edit, button_type): my_thread = MyThread(line_edit, button_type) my_thread.finished_signal.connect(self.update_line_edit) self.thread_list.append(my_thread) my_thread.start()
根据提示,可能是因为在不同的线程中尝试停止定时器。你可以尝试在调用 `start()` 方法时,将 `QTimer` 对象传递给 `ImageThread` 类,并在 `ImageThread` 中使用该定时器对象,而不是在 `MyWindow` 类中创建定时器对象。这样可以确保定时器在同一线程中启动和停止,避免出现跨线程问题。
另外,你在 `MyWindow` 类中定义了 `saved_color_photos` 和 `saved_depth_photos` 这两个属性,但是没有看到它们被使用。如果你想使用这两个属性,可以在 `update_saved_color_photos_label` 和 `update_saved_depth_photos_label` 方法中更新它们的值,并在需要时获取它们的值。例如:
```
def update_saved_color_photos_label(self, num_photos):
self.saved_color_photos += num_photos
self.ui.label_5.setText(f"已保存 {self.saved_color_photos} 张")
def update_saved_depth_photos_label(self, num_photos):
self.saved_depth_photos += num_photos
self.ui.label_6.setText(f"已保存 {self.saved_depth_photos} 张")
```
这样在每次保存颜色或深度图像时,`saved_color_photos` 和 `saved_depth_photos` 的值都会相应地增加,并在界面上显示出来。
使用QTimer对象代替QBasicTimer对象,修改程序class MyWindow(QWidget): def init(self): super().init() self.thread_list = [] self.color_photo_dir = os.path.join(os.getcwd(), "color_photos") self.depth_photo_dir = os.path.join(os.getcwd(), "depth_photos") self.image_thread = None self.saved_color_photos = 0 # 定义 saved_color_photos 属性 self.saved_depth_photos = 0 # 定义 saved_depth_photos 属性 self.init_ui() def init_ui(self): self.ui = uic.loadUi("C:/Users/wyt/Desktop/D405界面/intelrealsense1.ui") self.open_btn = self.ui.pushButton self.color_image_chose_btn = self.ui.pushButton_3 self.depth_image_chose_btn = self.ui.pushButton_4 self.open_btn.clicked.connect(self.open) self.color_image_chose_btn.clicked.connect(lambda: self.chose_dir(self.ui.lineEdit, "color")) self.depth_image_chose_btn.clicked.connect(lambda: self.chose_dir(self.ui.lineEdit_2, "depth")) def open(self): self.profile = self.pipeline.start(self.config) self.is_camera_opened = True self.label.setText('相机已打开') self.label.setStyleSheet('color:green') self.open_btn.setEnabled(False) self.close_btn.setEnabled(True) self.image_thread = ImageThread(self.pipeline, self.color_label, self.depth_label, self.interval, self.color_photo_dir, self.depth_photo_dir, self._dgl) self.image_thread.saved_color_photos_signal.connect(self.update_saved_color_photos_label) self.image_thread.saved_depth_photos_signal.connect(self.update_saved_depth_photos_label) self.image_thread.start() def chose_dir(self, line_edit, button_type): my_thread = MyThread(line_edit, button_type) my_thread.finished_signal.connect(self.update_line_edit) self.thread_list.append(my_thread) my_thread.start()
以下是使用QTimer对象代替QBasicTimer对象的修改后的代码:
```python
from PyQt5.QtCore import QTimer
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.thread_list = []
self.color_photo_dir = os.path.join(os.getcwd(), "color_photos")
self.depth_photo_dir = os.path.join(os.getcwd(), "depth_photos")
self.image_thread = None
self.saved_color_photos = 0
self.saved_depth_photos = 0
self.init_ui()
def init_ui(self):
self.ui = uic.loadUi("C:/Users/wyt/Desktop/D405界面/intelrealsense1.ui")
self.open_btn = self.ui.pushButton
self.color_image_chose_btn = self.ui.pushButton_3
self.depth_image_chose_btn = self.ui.pushButton_4
self.open_btn.clicked.connect(self.open)
self.color_image_chose_btn.clicked.connect(lambda: self.chose_dir(self.ui.lineEdit, "color"))
self.depth_image_chose_btn.clicked.connect(lambda: self.chose_dir(self.ui.lineEdit_2, "depth"))
def open(self):
self.profile = self.pipeline.start(self.config)
self.is_camera_opened = True
self.label.setText('相机已打开')
self.label.setStyleSheet('color:green')
self.open_btn.setEnabled(False)
self.close_btn.setEnabled(True)
self.image_thread = ImageThread(self.pipeline, self.color_label, self.depth_label, self.interval, self.color_photo_dir, self.depth_photo_dir, self._dgl)
self.image_thread.saved_color_photos_signal.connect(self.update_saved_color_photos_label)
self.image_thread.saved_depth_photos_signal.connect(self.update_saved_depth_photos_label)
self.image_thread.start()
self.timer = QTimer(self) # 创建QTimer对象
self.timer.timeout.connect(self.update) # 连接timeout信号与槽函数
self.timer.start(100) # 启动定时器,间隔为100ms
def update(self):
# 检查所有的线程是否已完成,并从线程列表中移除已完成的线程
for thread in self.thread_list:
if not thread.isRunning():
self.thread_list.remove(thread)
def chose_dir(self, line_edit, button_type):
my_thread = MyThread(line_edit, button_type)
my_thread.finished_signal.connect(self.update_line_edit)
self.thread_list.append(my_thread)
my_thread.start()
def update_saved_color_photos_label(self, count):
self.saved_color_photos = count
self.ui.label_5.setText(str(self.saved_color_photos))
def update_saved_depth_photos_label(self, count):
self.saved_depth_photos = count
self.ui.label_6.setText(str(self.saved_depth_photos))
```
阅读全文