欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5實(shí)現(xiàn)進(jìn)度條與定時(shí)器及子線程同步關(guān)聯(lián)

 更新時(shí)間:2023年01月12日 10:02:25   作者:Sir 老王  
這篇文章主要為大家詳細(xì)介紹了PyQt5如何實(shí)現(xiàn)進(jìn)度條與定時(shí)器及子線程的同步關(guān)聯(lián),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

進(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)文章

最新評(píng)論