PyQt5實現(xiàn)進度條與定時器及子線程同步關聯(lián)
進度條是當我們處理冗長的任務時使用的控件,它是以動畫的形式讓用戶知道該任務正在取得進展。
在PyQt5中的進度條對應組件是QProgressBar,該對象繼承自QWidget組件。一般在生產(chǎn)業(yè)務的實現(xiàn)過程中還需要借助定時器來實現(xiàn)的。
在實際使用的過程當中進度條的進度控制往往還要根據(jù)子線程的業(yè)務執(zhí)行過程來設置進度顯示。

PyQt5在之前的文章中已經(jīng)多次使用,想必大家都比較熟悉了。若是沒有安裝的話使用pip的方式安裝一下即可。
pip?install?PyQt5
接下來將本文中使用到的相關模塊都導入到代碼塊中。
#?It?imports?all?the?classes,?methods,?and?attributes?of?the?`PyQt5.QtWidgets`?module. from?PyQt5.QtWidgets?import?* #?It?imports?all?the?classes,?methods,?and?attributes?of?the?`PyQt5.QtCore`?module. from?PyQt5.QtCore?import?* #?It?imports?all?the?classes,?methods,?and?attributes?of?the?`PyQt5.QtGui`?module. from?PyQt5.QtGui?import?* #?It?imports?the?`sys`?module. import?sys #?It?imports?the?`time`?module?and?it?renames?it?to?`t`. import?time?as?t
接下來我們創(chuàng)建一個ProgressBarUI的python類,將所有的進度條的布局以及槽函數(shù)放到該類中。
#?This?class?is?a?widget?that?displays?a?progress?bar.
class?ProgressBarUI(QWidget):
????def?__init__(self):
????????super(ProgressBarUI,?self).__init__()
????????self.init_ui()
????def?init_ui(self):
????????"""
????????This?function?initializes?the?UI.
????????"""
????????self.setWindowTitle('PyQt5進度條案例?公眾號:Python 集中營')
????????self.setWindowIcon(QIcon('prop.png'))
????????self.resize(500,?200)
????????self.progressBar?=?QProgressBar()
????????self.progressBar.setValue(0)
????????self.progressBar.setAlignment(Qt.AlignCenter)
????????hbox?=?QHBoxLayout()
????????hbox.addWidget(self.progressBar)
????????self.timer?=?QTimer()
????????self.timer.timeout.connect(self.listen_step)
????????self.timer.start(100)
????????self.step?=?0
????????self.thread_?=?WorkThread()
????????self.thread_.thread_step.connect(self.step_add)
????????self.thread_.start()
????????self.setLayout(hbox)
????def?step_add(self,?n):
????????"""
????????It?adds?n?to?the?current?value?of?the?counter
????????:param?n:?the?number?of?steps?to?take
????????"""
????????self.step?=?self.step?+?n
????def?listen_step(self):
????????"""
????????>?The?function?`listen_step`?is?called?by?the?`listen`?function,?and?it?returns?the?next?step?in?the?conversation
????????"""
????????if?self.progressBar.value()?>=?100:
????????????self.timer.stop()
????????else:
????????????self.progressBar.setValue(self.step)
創(chuàng)建一個子線程WorkThread繼承自QThread用于處理所有的業(yè)務實現(xiàn)邏輯。
#?This?class?is?a?subclass?of?QThread?and?it's?used?to?create?a?thread?that?will?run?the?function?that?will?be?passed?to #?it class?WorkThread(QThread): ????#?A?signal?that?is?emitted?when?the?value?of?the?counter?changes. ????thread_step?=?pyqtSignal(int) ????def?__init__(self): ????????""" ????????A?constructor.?It?is?called?when?an?object?is?created?from?a?class?and?it?allows?the?class?to?initialize?the ????????attributes?of?a?class. ????????""" ????????super(WorkThread,?self).__init__() ????????self.working?=?True ????def?__del__(self): ????????""" ????????A?destructor.?It?is?called?when?the?object?is?destroyed. ????????""" ????????self.working?=?False ????def?run(self): ????????for?n?in?range(1,?101): ????????????self.thread_step.emit(1) ????????????t.sleep(0.1)
使用python模塊的main函數(shù),將整個應用加入到系統(tǒng)的主體循環(huán)中。
#?A?common?idiom?to?use?this?as?the?last?statement?in?a?module?(a?file?that?contains?Python?code). if?__name__?==?'__main__': ????app?=?QApplication(sys.argv) ????main?=?ProgressBarUI() ????main.show() ????sys.exit(app.exec_())
到此這篇關于PyQt5實現(xiàn)進度條與定時器及子線程同步關聯(lián)的文章就介紹到這了,更多相關PyQt5同步關聯(lián)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python的Flask框架應用調(diào)用Redis隊列數(shù)據(jù)的方法
這里為大家?guī)鞵ython的Flask框架應用調(diào)用Redis隊列數(shù)據(jù)的方法,從而能夠?qū)崿F(xiàn)異步無阻塞從而提高某些實時處理情況下程序的性能,需要的朋友可以參考下2016-06-06
通俗的講解深度學習中CUDA,cudatookit,cudnn和pytorch的關系
有些剛?cè)胄械呐笥芽偸歉悴磺宄﨏UDA,cudatookit,cudnn和pytorch的關系,那么今天這篇文章用通俗易懂的話講解了他們之間的關系,需要的朋友可以參考下,相信會對你有所幫助2023-03-03
python?pyvis庫創(chuàng)建可視化交互式網(wǎng)絡圖
這篇文章主要為大家介紹了python?pyvis庫創(chuàng)建可視化交互式網(wǎng)絡圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Python PyQt5實現(xiàn)拖拽與剪貼板功能詳解
這篇文章主要為大家詳細介紹了Python PyQt5如何實現(xiàn)拖拽與剪貼板功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-12-12

