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

PyQt5在之前的文章中已經(jīng)多次使用,想必大家都比較熟悉了。若是沒(méi)有安裝的話使用pip的方式安裝一下即可。
pip?install?PyQt5
接下來(lái)將本文中使用到的相關(guān)模塊都導(dǎo)入到代碼塊中。
#?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
接下來(lái)我們創(chuàng)建一個(gè)ProgressBarUI的python類,將所有的進(jìn)度條的布局以及槽函數(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進(jìn)度條案例?公眾號(hào):Python 集中營(yíng)')
????????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)建一個(gè)子線程WorkThread繼承自QThread用于處理所有的業(yè)務(wù)實(shí)現(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ù),將整個(gè)應(yīng)用加入到系統(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_())
到此這篇關(guān)于PyQt5實(shí)現(xiàn)進(jìn)度條與定時(shí)器及子線程同步關(guān)聯(lián)的文章就介紹到這了,更多相關(guān)PyQt5同步關(guān)聯(lián)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
win7 下搭建sublime的python開(kāi)發(fā)環(huán)境的配置方法
Sublime Text具有漂亮的用戶界面和強(qiáng)大的功能,例如代碼縮略圖,Python的插件,代碼段等。還可自定義鍵綁定,菜單和工具欄。Sublime Text的主要功能包括:拼寫檢查,書(shū)簽,完整的 Python API,Goto功能,即時(shí)項(xiàng)目切換,多選擇,多窗口等等。2014-06-06
python實(shí)現(xiàn)凱撒密碼加密解密的示例代碼
本文主要介紹了python實(shí)現(xiàn)凱撒密碼加密解密的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
pytorch利用Dataset讀取數(shù)據(jù)報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了pytorch利用Dataset讀取數(shù)據(jù)報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
利用python3隨機(jī)生成中文字符的實(shí)現(xiàn)方法
最近在學(xué)習(xí)python3,發(fā)現(xiàn)網(wǎng)上關(guān)于ptyhon3隨機(jī)生成中文的資料非常少,所以決定將自己實(shí)現(xiàn)的方法分享下,下面這篇文章主要給大家介紹了關(guān)于利用python3隨機(jī)生成中文字符的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-11-11
在Python的Django框架中創(chuàng)建語(yǔ)言文件
這篇文章主要介紹了在Python的Django框架中創(chuàng)建語(yǔ)言文件的方法,以語(yǔ)言代碼來(lái)表示語(yǔ)言區(qū)域種類,需要的朋友可以參考下2015-07-07

