程序运行提示AttributeError: 'MyWindow' object has no attribute 'saved_color_photos',优化程序class MyWindow(QWidget): def __init__(self): super().__init__() self.thread_list = [] self.image_thread = None self.init_ui() def init_ui(self): self.ui = uic.loadUi("C:/Users/wyt/Desktop/D405界面/intelrealsense1.ui") 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 close(self): self.image_thread.stop() self.pipeline.stop() self.is_camera_opened = False self.label.setText('相机未打开') self.label.setStyleSheet('color:red') self.open_btn.setEnabled(True) self.close_btn.setEnabled(False) print("已保存color_image的数量是:%d" % self.saved_color_photos) print("已保存depth_image的数量是:%d" % self.saved_depth_photos) self.hide() # 将窗口隐藏,而非退出程序
时间: 2024-01-30 09:04:04 浏览: 37
根据错误提示,似乎你在某个地方使用了 `self.saved_color_photos`,但是 `MyWindow` 类中并没有定义这个属性,因此出现了 `AttributeError`。你需要在 `__init__()` 方法中定义 `self.saved_color_photos` 和 `self.saved_depth_photos` 两个属性,例如:
```python
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.thread_list = []
self.image_thread = None
self.saved_color_photos = 0 # 定义 saved_color_photos 属性
self.saved_depth_photos = 0 # 定义 saved_depth_photos 属性
self.init_ui()
# 省略其他方法
```
这样就可以避免出现上述错误了。
相关问题
程序运行后提示AttributeError: 'ImageThread' object has no attribute 'saved_color_photos_label',优化上述程序
AttributeError: 'ImageThread' object has no attribute 'saved_color_photos_label' 的错误提示说明程序中的 ImageThread 类对象没有 saved_color_photos_label 属性,可能是因为该属性未被正确初始化或者被误删导致的。为了优化程序,我们需要对该部分代码进行调整。
可以尝试在 ImageThread 类的 __init__ 方法中添加 saved_color_photos_label 属性的初始化语句。例如:
```
class ImageThread(threading.Thread):
def __init__(self, image_path, color_photos_label):
threading.Thread.__init__(self)
self.image_path = image_path
self.color_photos_label = color_photos_label
self.saved_color_photos_label = None # 添加该行代码
def run(self):
# 图像处理代码
# ...
```
这样,当 ImageThread 类对象被创建时,saved_color_photos_label 属性会被初始化为 None。在图像处理过程中,可以根据需要修改该属性的值。
AttributeError: 'ImageThread' object has no attribute 'saved_color_photos'
这个错误是因为你的 `ImageThread` 类中没有一个名为 `saved_color_photos` 的属性,但是你在某处使用了它。
你需要检查你的代码,查看在哪里使用了 `saved_color_photos` 属性,然后确认是否正确地定义了该属性。你可以在 `ImageThread` 类的构造函数中为其设置初始值,例如:
```
class ImageThread(Thread):
def __init__(self):
super(ImageThread, self).__init__()
self.saved_color_photos = []
```
这样就会在创建 `ImageThread` 实例时初始化 `saved_color_photos` 属性为空列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)