python使用tkinter包實(shí)現(xiàn)進(jìn)度條
python中的tkinter包是一種常見(jiàn)的設(shè)計(jì)程序的GUI界面用的包。本文主要介紹這里面的一個(gè)組件:進(jìn)度條(Progressbar)。Tkinter Progressbar里面對(duì)進(jìn)度條組件已經(jīng)做了一定的介紹,但比較抽象。本文以另一種方式介紹這個(gè)組件及其常用用法。
一、進(jìn)度條的基本概念
進(jìn)度條組件涉及多個(gè)基本概念,但本文主要介紹幾個(gè)最重要的概念。而至于其它概念,多半只是涉及一些細(xì)節(jié)優(yōu)化。
(一)進(jìn)度條模式
tkinter支持兩種模式的進(jìn)度條:
1. determinate模式
這種模式的進(jìn)度條通常是這樣的:
也就是說(shuō),這樣的進(jìn)度條適用于任務(wù)的完成進(jìn)度明確,且可以量化的情況。
2. indeterminate模式
這種模式的進(jìn)度條通常是這樣的:
也就是說(shuō),這樣的進(jìn)度條適用于任務(wù)的完成進(jìn)度不明確或無(wú)法量化的情況。進(jìn)度條的滾動(dòng)只能表示任務(wù)正在運(yùn)行中,無(wú)法表示任務(wù)完成了多少。
(二)進(jìn)度條綁定的變量
進(jìn)度條組件通常要綁定一個(gè)數(shù)值型變量,如Int或Double,這個(gè)變量存放進(jìn)度條的進(jìn)度數(shù)值。
對(duì)于determinate模式下的進(jìn)度條,數(shù)值通常從0開(kāi)始,隨著進(jìn)度的發(fā)展不斷升高(如何升高將在下一節(jié)進(jìn)行說(shuō)明),但到達(dá)99后,繼續(xù)升高將清零,重新從0開(kāi)始。也就是說(shuō),如果進(jìn)度量是x,那么該數(shù)值變量的值是x%100。(%表示取余)但數(shù)值可以手動(dòng)調(diào)為100,此時(shí)進(jìn)度條滿(mǎn)格。
對(duì)于indeterminate模式下的進(jìn)度條,數(shù)值通常從0開(kāi)始,隨著進(jìn)度的發(fā)展不斷升高,沒(méi)有止境。
以上是對(duì)基本概念的介紹。下面貼上進(jìn)度條組件相關(guān)的代碼進(jìn)行進(jìn)一步說(shuō)明。
prgVar = DoubleVar(value=0) prgb = ttk.Progressbar(root, orient='horizontal', length=200, mode='determinate', variable=prgVar) prgb.grid(row=0, columnspan=4)
在對(duì)象prgb里,variable參數(shù)對(duì)應(yīng)的變量是prgVar,所以prgVar是進(jìn)度條組件prgb綁定的變量。這個(gè)組件的模式是determinate。
用一個(gè)標(biāo)簽組件(Label)顯示該變量的數(shù)值。
labl = Label(root, textvariable=prgVar) labl.grid(row=2, columnspan=4)
效果如下:
如動(dòng)畫(huà)中所示,變量值到達(dá)99后退回了0,重新開(kāi)始。
但如果是indeterminate模式,則是另一種情況。
prgbInfVar = IntVar(value=0) prgbInf = ttk.Progressbar(root, orient="horizontal", length=200, mode='indeterminate', variable=prgbInfVar) prgbInf.grid(row=3, columnspan=4) lablInf = Label(root, textvariable=prgbInfVar) lablInf.grid(row=5, columnspan=4)
效果如下:
如動(dòng)畫(huà)中所示,變量值一直在不斷增加。
(三)進(jìn)度條的動(dòng)作
前面所示的那些動(dòng)畫(huà)中,進(jìn)度條都是以一個(gè)速度在遞進(jìn),數(shù)值也在增加。當(dāng)然,進(jìn)度條的數(shù)值可以通過(guò)人工調(diào)整其對(duì)應(yīng)變量的值來(lái)設(shè)置,進(jìn)度條的顯示也會(huì)隨數(shù)值而變化。
例如,要把determinate的進(jìn)度條清空,只需將進(jìn)度條綁定的變量值設(shè)為0即可。
prgVar.set(0)
此時(shí)進(jìn)度條變空。
于此同時(shí),進(jìn)度條有start,step,以及stop三個(gè)常用動(dòng)作。
prgb.start(p) prgb.step(x) prgb.stop()
以上三行代碼,分別表示:
1. 進(jìn)度條prgb開(kāi)始自動(dòng)遞增,每隔p毫秒遞增1。
2. 進(jìn)度條prgb一次性遞增x。該動(dòng)作和進(jìn)度條當(dāng)前是否在自動(dòng)遞增無(wú)關(guān),執(zhí)行后也不會(huì)影響進(jìn)度條是否繼續(xù)自動(dòng)遞增。
3. 進(jìn)度條prgb停止自動(dòng)遞增。注意該動(dòng)作并不自動(dòng)將進(jìn)度條清零。
注意:若進(jìn)度條的模式是determinate,以上動(dòng)作執(zhí)行時(shí)仍然遵守進(jìn)度條變量超過(guò)99清零的規(guī)則。
二、程序示例
現(xiàn)在用一個(gè)例子來(lái)幫助讀者復(fù)習(xí)進(jìn)度條的相關(guān)知識(shí)。
有很多人在設(shè)計(jì)程序時(shí),希望實(shí)現(xiàn)這樣的顯示:
當(dāng)任務(wù)尚未開(kāi)始時(shí),進(jìn)度條應(yīng)為空
當(dāng)任務(wù)正在進(jìn)行時(shí),由于任務(wù)的進(jìn)度無(wú)法確認(rèn)或量化,只能用indeterminate模式的進(jìn)度條表示
當(dāng)任務(wù)失敗時(shí),進(jìn)度條回到空的狀態(tài)。但當(dāng)任務(wù)完成時(shí),進(jìn)度條應(yīng)為滿(mǎn)
這樣的邏輯如何實(shí)現(xiàn)呢?對(duì)于indeterminate模式下的進(jìn)度條,無(wú)論綁定的變量值為多少,都不可能實(shí)現(xiàn)進(jìn)度條為空或滿(mǎn)的顯示。
此時(shí),就需要將進(jìn)度條的模式轉(zhuǎn)變?yōu)閐eterminate
prgbInf['mode'] = 'determinate'
在這里設(shè)計(jì)一個(gè)程序,實(shí)現(xiàn)這樣的邏輯:
完整的代碼:
from tkinter import * from tkinter import ttk root = Tk() root.geometry('250x250') root.title('Progress bar') def startProgress(): prgb.start(100) #increment 1 per 100ms #labl['text'] = prgb['value'] def stepProgress(): prgb.step(20) #increment 20 #labl['text'] = prgb['value'] def stopProgress(): prgb.stop() #labl['text'] = prgb['value'] def clearProgress(): prgVar.set(0) def startProgressInf(): prgbInf['mode'] = 'indeterminate' prgbInf.start(30) #increment 1 per 100ms #labl['text'] = prgb['value'] def finishProgressInf(): prgbInfVar.set(100) prgbInf.stop() prgbInf['mode'] = 'determinate' #labl['text'] = prgb['value'] def stopProgressInf(): prgbInf['mode'] = 'indeterminate' prgbInf.stop() #labl['text'] = prgb['value'] def clearProgressInf(): prgbInfVar.set(0) prgbInf.stop() prgbInf['mode'] = 'determinate' prgVar = DoubleVar(value=0) prgb = ttk.Progressbar(root, orient='horizontal', length=200, mode='determinate', variable=prgVar) #If progress bar has determined progress value, then use this prgb.grid(row=0, columnspan=4) btStart = Button(root, text="Start", command=startProgress) btStart.grid(row=1, column=0) btStep = Button(root, text="Step", command=stepProgress) btStep.grid(row=1, column=1) btStop = Button(root, text="Stop", command=stopProgress) btStop.grid(row=1, column=2) btClear = Button(root, text="Clear", command=clearProgress) btClear.grid(row=1, column=3) labl = Label(root, textvariable=prgVar) labl.grid(row=2, columnspan=4) prgbInfVar = IntVar(value=0) prgbInf = ttk.Progressbar(root, orient="horizontal", length=200, mode='indeterminate', variable=prgbInfVar) prgbInf.grid(row=3, columnspan=4) btStartInf = Button(root, text="Start", command=startProgressInf) btStartInf.grid(row=4, column=0) btFinishInf = Button(root, text="Finish", command=finishProgressInf) btFinishInf.grid(row=4, column=1) btStopInf = Button(root, text="Stop", command=stopProgressInf) btStopInf.grid(row=4, column=2) btClearInf = Button(root, text="Clear", command=clearProgressInf) btClearInf.grid(row=4, column=3) lablInf = Label(root, textvariable=prgbInfVar) lablInf.grid(row=5, columnspan=4) #So, firstly, start, stop, step works for both determinate progress bar and indeterminate progress bar #secondly, for determinate progress bar the value loops from 0 to 100, reset when full #but for indeterminate progress bar, the value keeps increasing. root.mainloop()
在這個(gè)程序里,btStartInf按鈕實(shí)現(xiàn)了任務(wù)正在進(jìn)行時(shí)的進(jìn)度條,btClearInf按鈕實(shí)現(xiàn)了任務(wù)失敗的進(jìn)度條,btFinishInf按鈕實(shí)現(xiàn)了任務(wù)完成的進(jìn)度條。具體實(shí)現(xiàn)方式,見(jiàn)按鈕對(duì)應(yīng)的函數(shù)。
到此這篇關(guān)于python使用tkinter包實(shí)現(xiàn)進(jìn)度條的文章就介紹到這了,更多相關(guān)python tkinter進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中opencv圖像疊加、圖像融合、按位操作的具體實(shí)現(xiàn)
opencv圖像操作可以更好更快的方便我們處理圖片,本文主要介紹了圖像疊加、圖像融合、按位操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07簡(jiǎn)述python四種分詞工具,盤(pán)點(diǎn)哪個(gè)更好用?
這篇文章主要介紹了python四種分詞工具的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04Python通過(guò)getattr函數(shù)獲取對(duì)象的屬性值
這篇文章主要介紹了Python通過(guò)getattr函數(shù)獲取對(duì)象的屬性值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10