栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

直接的问题是那

Ui_MainWindow
是一个类,而不是该类的实例。您必须将“窗口”
self
传递给
DownloadThread
。但这毕竟不是正确的解决方案。您无法从其他线程访问PyQt小部件。相反,请使用与您已经使用的相同的技术来更新状态文本(
带有文本标签的FTP下载,其中显示了下载的当前状态 )。

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 = Trueclass 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
  • 无需
    DownloadThread
    从继承
    Ui_MainWindow


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号