从另一个运行FTP下载的线程更新PyQt进度

2024-09-28 22:24:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从另一个类/线程(DownloadThread()类)访问进度条(在Ui_MainWindow()类中)setMaximum()。在

我试图使DownloadThread()类从Ui_MainWindow继承: DownloadThread(Ui_MainWindow)。但当我尝试设置最大进度条值时:

Ui_MainWindow.progressBar.setMaximum(100)

我得到这个错误:

AttributeError: type object 'Ui_MainWindow' has no attribute 'progressBar'

我的代码:

^{pr2}$

Tags: no代码进度条uiobjecttype错误attribute
2条回答

线程类:

from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport,QtWebEngineWidgets
from PyQt5.QtWidgets import QDialog,QWidget,QApplication, QInputDialog, QLineEdit, QFileDialog,QProgressDialog, QMainWindow, QFrame,QSplashScreen
from PyQt5.QtCore import QThread , pyqtSignal,Qt
from PyQt5.QtGui import QIcon,QPainter,QPixmap

class threaded_class(QThread):

    signal_to_send_at_progress_bar = pyqtSignal()
    def __init__(self,parent=None):
        QThread.__init__(self, parent=parent)
    def run(self):
        while self.isRunning:
            ##do the stuf you want here and when you want to change the progress bar
            self.signal_to_send_at_progress_bar.emit()

在主窗口中:

^{pr2}$

也可以用信号发出字符串和数字。在

直接的问题是Ui_MainWindow是一个类,而不是该类的实例。您必须将您的“窗口”self传递给DownloadThread。但无论如何,这不是正确的解决办法。不能从另一个线程访问PyQt小部件。相反,使用与您已经做过的相同的技术来更新状态文本(FTP download with text label showing the current status of the download)。在

class Ui_MainWindow(object):
    def download_file(self):
        self.thread = DownloadThread()
        self.thread.data_downloaded.connect(self.on_data_ready)
        self.thread.data_progress.connect(self.on_progress_ready)
        self.progress_initialized = False
        self.thread.start()

    def on_progress_ready(self, data):
        # The first signal sets the maximum, the other signals increase a progress
        if self.progress_initialized:
            self.progressBar.setValue(self.progressBar.value() + int(data))
        else:
            self.progressBar.setMaximum(int(data))
            self.progress_initialized = True

class DownloadThread(QtCore.QThread):

    data_downloaded = QtCore.pyqtSignal(object)
    data_progress = QtCore.pyqtSignal(object)

    def run(self):
        self.data_downloaded.emit('Status: Connecting...')

        with FTP('example.com') as ftp:
            ftp.login(user='user', passwd='pass')

            ftp.cwd('/some_directory/')

            filename = '100MB.bin'
            totalsize = ftp.size(filename)
            print(totalsize)

            # The first signal sets the maximum
            self.data_progress.emit(str(totalsize))

            self.data_downloaded.emit('Status: Downloading...')

            with open(filename, 'wb') as self.localfile:
                ftp.retrbinary('RETR ' + filename, self.file_write)

        self.data_downloaded.emit('Status: Updated!')

    def file_write(self, data):
        self.localfile.write(data)
        # The other signals increase a progress
        self.data_progress.emit(str(len(data)))

对代码的其他更改:

  • global localfile是一种不好的做法。请改用self.localfile。在
  • 不需要localfile.close()with来处理。在
  • 类似地,ftp.quit()应替换为with。在
  • 不需要DownloadThreadUi_MainWindow继承。在

相关问题 更多 >