PyQt5實(shí)現(xiàn)進(jìn)度條與定時(shí)器及子線程同步關(guān)聯(lián)
進(jìn)度條是當(dāng)我們處理冗長的任務(wù)時(shí)使用的控件,它是以動(dòng)畫的形式讓用戶知道該任務(wù)正在取得進(jìn)展。
在PyQt5中的進(jìn)度條對(duì)應(yīng)組件是QProgressBar,該對(duì)象繼承自QWidget組件。一般在生產(chǎn)業(yè)務(wù)的實(shí)現(xiàn)過程中還需要借助定時(shí)器來實(shí)現(xiàn)的。
在實(shí)際使用的過程當(dāng)中進(jìn)度條的進(jìn)度控制往往還要根據(jù)子線程的業(yè)務(wù)執(zhí)行過程來設(shè)置進(jìn)度顯示。
PyQt5在之前的文章中已經(jīng)多次使用,想必大家都比較熟悉了。若是沒有安裝的話使用pip的方式安裝一下即可。
pip?install?PyQt5
接下來將本文中使用到的相關(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
接下來我們創(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 集中營') ????????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)文章
代碼解析python標(biāo)準(zhǔn)庫logging模塊
這篇文章主要為大家介紹了代碼解析python標(biāo)準(zhǔn)庫logging模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python實(shí)現(xiàn)圖像隨機(jī)裁剪的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)圖像隨機(jī)裁剪的示例代碼,幫助大家更好的理解和使用python處理圖片,感興趣的朋友可以了解下2020-12-12Python的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法
這里為大家?guī)鞵ython的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法,從而能夠?qū)崿F(xiàn)異步無阻塞從而提高某些實(shí)時(shí)處理情況下程序的性能,需要的朋友可以參考下2016-06-06通俗的講解深度學(xué)習(xí)中CUDA,cudatookit,cudnn和pytorch的關(guān)系
有些剛?cè)胄械呐笥芽偸歉悴磺宄﨏UDA,cudatookit,cudnn和pytorch的關(guān)系,那么今天這篇文章用通俗易懂的話講解了他們之間的關(guān)系,需要的朋友可以參考下,相信會(huì)對(duì)你有所幫助2023-03-03python?pyvis庫創(chuàng)建可視化交互式網(wǎng)絡(luò)圖
這篇文章主要為大家介紹了python?pyvis庫創(chuàng)建可視化交互式網(wǎng)絡(luò)圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python PyQt5實(shí)現(xiàn)拖拽與剪貼板功能詳解
這篇文章主要為大家詳細(xì)介紹了Python PyQt5如何實(shí)現(xiàn)拖拽與剪貼板功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12