久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從另一個(gè)運(yùn)行 FTP 下載的線程更新 PyQt 進(jìn)度

Update PyQt progress from another thread running FTP download(從另一個(gè)運(yùn)行 FTP 下載的線程更新 PyQt 進(jìn)度)
本文介紹了從另一個(gè)運(yùn)行 FTP 下載的線程更新 PyQt 進(jìn)度的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想從另一個(gè)類/線程訪問(wèn)進(jìn)度條(在 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).但是當(dāng)我嘗試設(shè)置最大進(jìn)度條值時(shí):

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)

我收到此錯(cuò)誤:

AttributeError:類型對(duì)象Ui_MainWindow"沒(méi)有屬性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))

推薦答案

直接的問(wèn)題是 Ui_MainWindow 是一個(gè)類,而不是類的實(shí)例.您必須將窗口"self 傳遞給 DownloadThread.但這無(wú)論如何都不是正確的解決方案.您不能從另一個(gè)線程訪問(wèn) PyQt 小部件.相反,使用您已經(jīng)使用的相同技術(shù)來(lái)更新?tīng)顟B(tài)文本(FTP 下載,帶有顯示當(dāng)前狀態(tài)的文本標(biāo)簽下載).

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)))

對(duì)代碼的其他更改:

  • global localfile 是一種不好的做法.請(qǐng)改用 self.localfile.
  • 不需要 localfile.close()with 可以解決這個(gè)問(wèn)題.
  • 類似地,ftp.quit() 應(yīng)該替換為 with.
  • DownloadThread 無(wú)需從 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.

這篇關(guān)于從另一個(gè)運(yùn)行 FTP 下載的線程更新 PyQt 進(jìn)度的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開(kāi)頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒(méi)有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動(dòng)對(duì)象不可下標(biāo))
主站蜘蛛池模板: 成人黄在线观看 | 天天色综| 国产内谢 | 高清视频一区二区三区 | 国产精品久久久久一区二区三区 | 日本一道本视频 | 九九看片 | 日韩精品免费视频 | 日本超碰在线 | 国产精品欧美精品日韩精品 | 国产成人精品一区二区三区 | 人人人干| 国产在线精品一区二区三区 | 国产一区三区在线 | 99国产精品一区二区三区 | 欧美日韩亚洲三区 | 成人福利在线观看 | 成人在线小视频 | 国产精品一区二区免费 | 国产成人午夜电影网 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 亚洲第1页 | 视频在线亚洲 | 亚洲视频免费在线观看 | 国产日韩欧美一区二区 | 天天爽夜夜爽精品视频婷婷 | 成人免费视屏 | 成人精品鲁一区一区二区 | 视频在线一区 | 日本不卡免费新一二三区 | 亚洲日本一区二区三区四区 | 在线看av网址 | 免费国产成人av | 在线一区 | 欧美aⅴ片 | h视频在线观看免费 | av网站免费 | 久久精品亚洲精品国产欧美 | 欧美无乱码久久久免费午夜一区 | 欧美一a| 大香在线伊779 |