榆林做网站需要注意的几点,梵克雅宝戒指,济南网站备案,宜春公司网站建设python中的tkinter包是一种常见的设计程序的GUI界面用的包。本文主要介绍这里面的一个组件#xff1a;进度条#xff08;Progressbar#xff09;。Tkinter Progressbar里面对进度条组件已经做了一定的介绍#xff0c;但比较抽象。本文以另一种方式介绍这个组件及其常用用法…python中的tkinter包是一种常见的设计程序的GUI界面用的包。本文主要介绍这里面的一个组件进度条Progressbar。Tkinter Progressbar里面对进度条组件已经做了一定的介绍但比较抽象。本文以另一种方式介绍这个组件及其常用用法。
一、进度条的基本概念
进度条组件涉及多个基本概念但本文主要介绍几个最重要的概念。而至于其它概念多半只是涉及一些细节优化。
一进度条模式
tkinter支持两种模式的进度条
1. determinate模式
这种模式的进度条通常是这样的 也就是说这样的进度条适用于任务的完成进度明确且可以量化的情况。
2. indeterminate模式
这种模式的进度条通常是这样的 也就是说这样的进度条适用于任务的完成进度不明确或无法量化的情况。进度条的滚动只能表示任务正在运行中无法表示任务完成了多少。
二进度条绑定的变量
进度条组件通常要绑定一个数值型变量如Int或Double这个变量存放进度条的进度数值。
对于determinate模式下的进度条数值通常从0开始随着进度的发展不断升高如何升高将在下一节进行说明但到达99后继续升高将清零重新从0开始。也就是说如果进度量是那么该数值变量的值是。表示取余但数值可以手动调为100此时进度条满格。
对于indeterminate模式下的进度条数值通常从0开始随着进度的发展不断升高没有止境。
以上是对基本概念的介绍。下面贴上进度条组件相关的代码进行进一步说明。
prgVar DoubleVar(value0)
prgb ttk.Progressbar(root, orienthorizontal, length200, modedeterminate, variableprgVar)
prgb.grid(row0, columnspan4)
在对象prgb里variable参数对应的变量是prgVar所以prgVar是进度条组件prgb绑定的变量。这个组件的模式是determinate。
用一个标签组件Label显示该变量的数值。
labl Label(root, textvariableprgVar)
labl.grid(row2, columnspan4)
效果如下 如动画中所示变量值到达99后退回了0重新开始。
但如果是indeterminate模式则是另一种情况。
prgbInfVar IntVar(value0)
prgbInf ttk.Progressbar(root, orienthorizontal, length200, modeindeterminate, variableprgbInfVar)
prgbInf.grid(row3, columnspan4)
lablInf Label(root, textvariableprgbInfVar)
lablInf.grid(row5, columnspan4)
效果如下 如动画中所示变量值一直在不断增加。
三进度条的动作
前面所示的那些动画中进度条都是以一个速度在递进数值也在增加。当然进度条的数值可以通过人工调整其对应变量的值来设置进度条的显示也会随数值而变化。
例如要把determinate的进度条清空只需将进度条绑定的变量值设为0即可。
prgVar.set(0)
此时进度条变空。 于此同时进度条有startstep以及stop三个常用动作。
prgb.start(p)
prgb.step(x)
prgb.stop()
以上三行代码分别表示
1. 进度条prgb开始自动递增每隔p毫秒递增1。
2. 进度条prgb一次性递增x。该动作和进度条当前是否在自动递增无关执行后也不会影响进度条是否继续自动递增。
3. 进度条prgb停止自动递增。注意该动作并不自动将进度条清零。
注意若进度条的模式是determinate以上动作执行时仍然遵守进度条变量超过99清零的规则。
二、程序示例
现在用一个例子来帮助读者复习进度条的相关知识。
有很多人在设计程序时希望实现这样的显示
当任务尚未开始时进度条应为空 当任务正在进行时由于任务的进度无法确认或量化只能用indeterminate模式的进度条表示 当任务失败时进度条回到空的状态。但当任务完成时进度条应为满 这样的逻辑如何实现呢对于indeterminate模式下的进度条无论绑定的变量值为多少都不可能实现进度条为空或满的显示。
此时就需要将进度条的模式转变为determinate
prgbInf[mode] determinate
在这里设计一个程序实现这样的逻辑 完整的代码
from tkinter import *
from tkinter import ttkroot 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] indeterminateprgbInf.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] indeterminateprgbInf.stop()#labl[text] prgb[value]
def clearProgressInf():prgbInfVar.set(0)prgbInf.stop()prgbInf[mode] determinateprgVar DoubleVar(value0)
prgb ttk.Progressbar(root, orienthorizontal, length200, modedeterminate, variableprgVar) #If progress bar has determined progress value, then use this
prgb.grid(row0, columnspan4)btStart Button(root, textStart, commandstartProgress)
btStart.grid(row1, column0)
btStep Button(root, textStep, commandstepProgress)
btStep.grid(row1, column1)
btStop Button(root, textStop, commandstopProgress)
btStop.grid(row1, column2)
btClear Button(root, textClear, commandclearProgress)
btClear.grid(row1, column3)labl Label(root, textvariableprgVar)
labl.grid(row2, columnspan4)prgbInfVar IntVar(value0)
prgbInf ttk.Progressbar(root, orienthorizontal, length200, modeindeterminate, variableprgbInfVar)
prgbInf.grid(row3, columnspan4)btStartInf Button(root, textStart, commandstartProgressInf)
btStartInf.grid(row4, column0)
btFinishInf Button(root, textFinish, commandfinishProgressInf)
btFinishInf.grid(row4, column1)
btStopInf Button(root, textStop, commandstopProgressInf)
btStopInf.grid(row4, column2)
btClearInf Button(root, textClear, commandclearProgressInf)
btClearInf.grid(row4, column3)lablInf Label(root, textvariableprgbInfVar)
lablInf.grid(row5, columnspan4)#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按钮实现了任务正在进行时的进度条btClearInf按钮实现了任务失败的进度条btFinishInf按钮实现了任务完成的进度条。具体实现方式见按钮对应的函数。