扬帆18 2026-03-03 15:13 采纳率: 50%
浏览 11

这里的self为什么错误

大家能不能帮我分析一下,这段代码为什么会报错:

我写了一个从远程主要下载图片的类:

class DownloadTask(QThread):
    all_done = pyqtSignal()

    def __init__(self, file_list, local_dir):
        super().__init__()
        self.file_list = file_list
        self.local_dir = local_dir
        self.semaphore = QSemaphore(5)  # 最大并发数
        self.completed = 0

    def run(self):
        if not self.file_list:
            self.all_done.emit()
            return

        threads = []
        for i, remote_path in enumerate(self.file_list):
            file_name=os.path.basename(remote_path)
            file_local_path=os.path.join(self.local_dir,file_name)
            thread = SingleDownload(remote_path, file_local_path, self.semaphore)
            thread.finished.connect(self.on_file_done)
            threads.append(thread)
            thread.start()

        for thread in threads:
            thread.wait()

        self.all_done.emit()

    def on_file_done(self):
        self.completed += 1
        # print(f"完成: {self.completed}/{len(self.file_list)}")


class SingleDownload(QThread):
    finished = pyqtSignal()

    def __init__(self, remote_path, local_path, semaphore):
        super().__init__()
        self.remote_path = remote_path
        self.local_path = local_path
        self.semaphore = semaphore

    def run(self):
        self.semaphore.acquire()
        try:
            # SSH下载逻辑
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(C.SERVER_HOST, username=C.SERVER_USER, password=C.SERVER_PASSWORD)

            sftp = ssh.open_sftp()
            os.makedirs(os.path.dirname(self.local_path), exist_ok=True)
            sftp.get(self.remote_path, self.local_path)
            sftp.close()
            ssh.close()

        except Exception as err:
            show_error_dialog(self,'远程访问失败!',f'{err}')
            print(err)
        finally:
            self.semaphore.release()
            self.finished.emit()

show_error_dialog是从其它模块导入的:


```python
def show_error_dialog(parent,title,message):
    """显示错误对话框"""
    QMessageBox.critical(parent, title, message)
    print_logs(resource_path(C.LOG_FILE_ADDRESS),message)
main文件中的调用语句为:
self.down = DownloadTask(download_images_address, root_dir)

现在下载出错时show_error_dialog(self,'远程访问失败!',f'{err}')会报错,我没明白是哪里有问题。
  • 写回答

3条回答 默认 最新

  • 扬帆18 2026-03-03 15:48
    关注

    报错为:

    Traceback (most recent call last):
      File "E:\pythonproject\ProductSystem\tools\thread_download_picture.py", line 68, in run
        show_error_dialog(self,'远程访问失败!',f'{err}')
      File "E:\pythonproject\ProductSystem\dataBase\showErrorDialog.py", line 8, in show_error_dialog
        QMessageBox.critical(parent, title, message)
    TypeError: critical(parent: Optional[QWidget], title: Optional[str], text: Optional[str], buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'SingleDownload'
    

    如果把self改写成parent=None,则进程卡住,最后崩溃。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月3日