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

從另一個運行 FTP 下載的線程更新 PyQt 進度

Update PyQt progress from another thread running FTP download(從另一個運行 FTP 下載的線程更新 PyQt 進度)
本文介紹了從另一個運行 FTP 下載的線程更新 PyQt 進度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想從另一個類/線程訪問進度條(在 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. 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模板網!

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

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 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:“浮動對象不可下標)
主站蜘蛛池模板: 久久国产99 | 黄色在线播放 | 久久av在线| 日韩在线观看中文字幕 | 欧美a级成人淫片免费看 | 欧美爱爱网 | 在线色网站 | 性色av浪潮av | 三级在线视频 | 在线观看的av | 91视频一区二区三区 | 狠狠干免费视频 | 不卡av在线播放 | 一区二区在线看 | 欧美国产精品 | 91麻豆精品国产91久久久久久久久 | 久久伊人av | 日日爽天天 | 国产精品美女在线 | 手机成人在线视频 | 精品欧美一区二区精品久久 | 日韩精品视频免费在线观看 | 天天拍天天干 | 国产丝袜一区 | 一级做a爱片性色毛片 | 久久免费影院 | 欧美日韩高清 | 日本熟妇毛耸耸xxxxxx | 欧美日韩性 | 国产一区二区中文字幕 | 天天干夜夜艹 | 国产欧美日韩在线观看 | 男女啪啪免费视频 | 欧美一道本 | 亚洲第一伊人 | 青青草免费观看 | 日韩精品久久久久久久 | 国产伊人网 | 毛片毛片毛片毛片毛片 | 国产精品福利一区 | 欧美日批视频 |