python使用tkinter包實現(xiàn)進度條
python中的tkinter包是一種常見的設(shè)計程序的GUI界面用的包。本文主要介紹這里面的一個組件:進度條(Progressbar)。Tkinter Progressbar里面對進度條組件已經(jīng)做了一定的介紹,但比較抽象。本文以另一種方式介紹這個組件及其常用用法。
一、進度條的基本概念
進度條組件涉及多個基本概念,但本文主要介紹幾個最重要的概念。而至于其它概念,多半只是涉及一些細節(jié)優(yōu)化。
(一)進度條模式
tkinter支持兩種模式的進度條:
1. determinate模式
這種模式的進度條通常是這樣的:
也就是說,這樣的進度條適用于任務(wù)的完成進度明確,且可以量化的情況。
2. indeterminate模式
這種模式的進度條通常是這樣的:
也就是說,這樣的進度條適用于任務(wù)的完成進度不明確或無法量化的情況。進度條的滾動只能表示任務(wù)正在運行中,無法表示任務(wù)完成了多少。
(二)進度條綁定的變量
進度條組件通常要綁定一個數(shù)值型變量,如Int或Double,這個變量存放進度條的進度數(shù)值。
對于determinate模式下的進度條,數(shù)值通常從0開始,隨著進度的發(fā)展不斷升高(如何升高將在下一節(jié)進行說明),但到達99后,繼續(xù)升高將清零,重新從0開始。也就是說,如果進度量是x,那么該數(shù)值變量的值是x%100。(%表示取余)但數(shù)值可以手動調(diào)為100,此時進度條滿格。
對于indeterminate模式下的進度條,數(shù)值通常從0開始,隨著進度的發(fā)展不斷升高,沒有止境。
以上是對基本概念的介紹。下面貼上進度條組件相關(guān)的代碼進行進一步說明。
prgVar = DoubleVar(value=0) prgb = ttk.Progressbar(root, orient='horizontal', length=200, mode='determinate', variable=prgVar) prgb.grid(row=0, columnspan=4)
在對象prgb里,variable參數(shù)對應(yīng)的變量是prgVar,所以prgVar是進度條組件prgb綁定的變量。這個組件的模式是determinate。
用一個標(biāo)簽組件(Label)顯示該變量的數(shù)值。
labl = Label(root, textvariable=prgVar) labl.grid(row=2, columnspan=4)
效果如下:
如動畫中所示,變量值到達99后退回了0,重新開始。
但如果是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)
效果如下:
如動畫中所示,變量值一直在不斷增加。
(三)進度條的動作
前面所示的那些動畫中,進度條都是以一個速度在遞進,數(shù)值也在增加。當(dāng)然,進度條的數(shù)值可以通過人工調(diào)整其對應(yīng)變量的值來設(shè)置,進度條的顯示也會隨數(shù)值而變化。
例如,要把determinate的進度條清空,只需將進度條綁定的變量值設(shè)為0即可。
prgVar.set(0)
此時進度條變空。
于此同時,進度條有start,step,以及stop三個常用動作。
prgb.start(p) prgb.step(x) prgb.stop()
以上三行代碼,分別表示:
1. 進度條prgb開始自動遞增,每隔p毫秒遞增1。
2. 進度條prgb一次性遞增x。該動作和進度條當(dāng)前是否在自動遞增無關(guān),執(zhí)行后也不會影響進度條是否繼續(xù)自動遞增。
3. 進度條prgb停止自動遞增。注意該動作并不自動將進度條清零。
注意:若進度條的模式是determinate,以上動作執(zhí)行時仍然遵守進度條變量超過99清零的規(guī)則。
二、程序示例
現(xiàn)在用一個例子來幫助讀者復(fù)習(xí)進度條的相關(guān)知識。
有很多人在設(shè)計程序時,希望實現(xiàn)這樣的顯示:
當(dāng)任務(wù)尚未開始時,進度條應(yīng)為空
當(dāng)任務(wù)正在進行時,由于任務(wù)的進度無法確認或量化,只能用indeterminate模式的進度條表示
當(dāng)任務(wù)失敗時,進度條回到空的狀態(tài)。但當(dāng)任務(wù)完成時,進度條應(yīng)為滿
這樣的邏輯如何實現(xiàn)呢?對于indeterminate模式下的進度條,無論綁定的變量值為多少,都不可能實現(xiàn)進度條為空或滿的顯示。
此時,就需要將進度條的模式轉(zhuǎn)變?yōu)閐eterminate
prgbInf['mode'] = 'determinate'
在這里設(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()
在這個程序里,btStartInf按鈕實現(xiàn)了任務(wù)正在進行時的進度條,btClearInf按鈕實現(xiàn)了任務(wù)失敗的進度條,btFinishInf按鈕實現(xiàn)了任務(wù)完成的進度條。具體實現(xiàn)方式,見按鈕對應(yīng)的函數(shù)。
到此這篇關(guān)于python使用tkinter包實現(xiàn)進度條的文章就介紹到這了,更多相關(guān)python tkinter進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中opencv圖像疊加、圖像融合、按位操作的具體實現(xiàn)
opencv圖像操作可以更好更快的方便我們處理圖片,本文主要介紹了圖像疊加、圖像融合、按位操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07Python通過getattr函數(shù)獲取對象的屬性值
這篇文章主要介紹了Python通過getattr函數(shù)獲取對象的屬性值,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10