問題描述
我想從另一個類/線程訪問進度條(在 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:
Ui_MainWindow.progressBar.setMaximum(100)
我收到此錯誤:
AttributeError:類型對象Ui_MainWindow"沒有屬性progressBar"
AttributeError: type object 'Ui_MainWindow' has no attribute 'progressBar'
我的代碼:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# ...
self.updateButton = QtGui.QPushButton(self.centralwidget)
self.progressBar = QtGui.QProgressBar(self.centralwidget)
self.updateStatusText = QtGui.QLabel(self.centralwidget)
# ...
self.updateButton.clicked.connect(self.download_file)
# ...
def download_file(self):
self.thread = DownloadThread()
self.thread.data_downloaded.connect(self.on_data_ready)
self.thread.start()
def on_data_ready(self, data):
self.updateStatusText.setText(str(data))
class DownloadThread(QtCore.QThread, Ui_MainWindow):
data_downloaded = QtCore.pyqtSignal(object)
def run(self):
self.data_downloaded.emit('Status: Connecting...')
ftp = FTP('example.com')
ftp.login(user='user', passwd='pass')
ftp.cwd('/some_directory/')
filename = '100MB.bin'
totalsize = ftp.size(filename)
print(totalsize)
# SET THE MAXIMUM VALUE OF THE PROGRESS BAR
Ui_MainWindow.progressBar.setMaximum(totalsize)
self.data_downloaded.emit('Status: Downloading...')
global localfile
with open(filename, 'wb') as localfile:
ftp.retrbinary('RETR ' + filename, self.file_write)
ftp.quit()
localfile.close()
self.data_downloaded.emit('Status: Updated!')
def file_write(self, data):
global localfile
localfile.write(data)
print(len(data))
推薦答案
直接的問題是 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).
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
. DownloadThread
無需從Ui_MainWindow
繼承.
global localfile
is a bad practice. Useself.localfile
instead.- There's no need for
localfile.close()
,with
takes care of that. - Similarly
ftp.quit()
should be replaced withwith
. - There's no need for
DownloadThread
to inherit fromUi_MainWindow
.
這篇關于從另一個運行 FTP 下載的線程更新 PyQt 進度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!