久久久久久久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:“浮動對象不可下標)
主站蜘蛛池模板: 天天爽夜夜操 | 精品久久久久久红码专区 | 九九热在线观看视频 | 国产色婷婷精品综合在线播放 | 亚洲精品字幕 | 中文字幕一区二区在线观看 | 欧美一二区 | 精品亚洲永久免费精品 | 亚洲成av人片在线观看 | 九九色综合| 香蕉超碰 | 亚洲欧美精品国产一级在线 | 日韩www视频 | 欧美视频一区 | 亚洲久在线 | 精品国产99 | 精品视频在线一区 | 亚洲精品国产精品国自产在线 | 国产999精品久久久久久绿帽 | 国产精品一区二区在线播放 | 亚洲国产激情 | 在线免费91| 午夜播放器在线观看 | www亚洲精品 | 国产精品99久久久久久久久久久久 | 国产黄色在线观看 | 色姑娘av | 国产视频亚洲视频 | 国产91一区 | 亚洲欧美自拍偷拍视频 | 久久久久91 | 亚洲狠狠| 欧美日韩精品中文字幕 | 亚洲一区精品视频 | 免费国产黄网站在线观看视频 | 色综网| 欧美一区二区三区在线播放 | 久久99精品久久久久蜜桃tv | 九九精品在线 | 中日韩欧美一级片 | xxx国产精品视频 |