看板 Python 關於我們 聯絡資訊
大家好,最近在學習PyQt,也透過RIP Tutorial的網頁資源中學習到QProgressBar的用法 詳細程式碼可參閱 http://tinyurl.com/y4fc8erp 透過網頁也了解QThread如何應用在QProgressBar 但自己心中有一個問題是,如果不用QThread,而改用threading.thread 有辦法達到同樣的效果嗎 因為threading.thread似乎沒有類似QThread的emit功能 因此我修改成用呼叫的方式去執行 (詳見個人程式碼中的Actions().onCountChanged(count)) 但並無法執行...,還是說要使用QProgressBar一定要搭配QThread才行 還麻煩各位指教,謝謝 個人修改的程式碼如下: import sys import time import threading from PyQt5.QtCore import QThread, pyqtSignal from PyQt5.QtWidgets import (QApplication, QDialog, QProgressBar, QPushButton) TIME_LIMIT = 100 class External(threading.Thread): def run(self): count = 0 while count < TIME_LIMIT: count +=1 time.sleep(0.1) print(count) Actions().onCountChanged(count) class Actions(QDialog): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Progress Bar') self.progress = QProgressBar(self) self.progress.setGeometry(0, 0, 300, 25) self.progress.setMaximum(100) self.button = QPushButton('Start', self) self.button.move(0, 30) self.show() self.button.clicked.connect(self.onButtonClick) def onButtonClick(self): self.calc = External() self.calc.start() def onCountChanged(self, value): self.progress.setValue(value) if __name__ == "__main__": app = QApplication(sys.argv) window = Actions() sys.exit(app.exec_()) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.22.64.151 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1558418151.A.91B.html
Raymond0710: Actions()這樣每次會new一個物件 不是window 05/21 22:02