问题描述
我想从另一个类/线程访问进度条(在 Ui_MainWindow()
类中)setMaximum()
(DownloadThread()类).
I want to access progress bar's (which is in the Ui_MainWindow()
class) setMaximum()
from another class/thread (DownloadThread()
class).
我尝试让 DownloadThread()
类继承自 Ui_MainWindow
:DownloadThread(Ui_MainWindow)
.但是当我尝试设置最大进度条值时:
I tried making DownloadThread()
class inherit from Ui_MainWindow
:
DownloadThread(Ui_MainWindow)
. But when I try to set the maximum progress bar value:
我收到此错误:
AttributeError:类型对象Ui_MainWindow"没有属性progressBar"
AttributeError: type object 'Ui_MainWindow' has no attribute 'progressBar'
我的代码:
推荐答案
直接的问题是 Ui_MainWindow
是一个类,而不是类的实例.您必须将窗口"self
传递给 DownloadThread
.但这无论如何都不是正确的解决方案.您不能从另一个线程访问 PyQt 小部件.相反,使用您已经使用的相同技术来更新状态文本(FTP 下载,带有显示当前状态的文本标签下载).
The immediate problem is that Ui_MainWindow
is a class, not an instance of the class. You would have to pass your "window" self
to the DownloadThread
. But that's not the right solution anyway. You cannot access PyQt widgets from another thread. Instead, use the same technique as you already do, to update the status text (FTP download with text label showing the current status of the download).
对代码的其他更改:
global localfile
是一种不好的做法.请改用 self.localfile
.- 不需要
localfile.close()
,with
可以解决这个问题. - 类似地,
ftp.quit()
应该替换为 with
. DownloadThread
无需从 Ui_MainWindow
继承.
global localfile
is a bad practice. Use self.localfile
instead.
- There's no need for
localfile.close()
, with
takes care of that.
- Similarly
ftp.quit()
should be replaced with with
.
- There's no need for
DownloadThread
to inherit from Ui_MainWindow
.
这篇关于从另一个运行 FTP 下载的线程更新 PyQt 进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
【网站声明】本站部分内容来源于互联网,旨在帮助大家更快的解决问题,如果有图片或者内容侵犯了您的权益,请联系我们删除处理,感谢您的支持!