赞
踩
本文主要讲述tkinter中进度条的使用。
progressbar = tkinter.ttk.Progressbar(root, mode="determinate", maximum=100, value=0)
progressbar.pack()
def updateProgressBar():
for i in range(100):
progressbar['value'] = i + 1
root.update()
time.sleep(0.1)
button = tkinter.Button(root, text='Running', command=updateProgressBar)
button.pack()
ttk.Progressbar
来创建确定进度条;
mode设置为:determinate;
设置maximum
和value
属性来控制进度条的进度。
root.update()来更新绘制页面
progress = tkinter.ttk.Progressbar(root, length=200, mode="indeterminate", orient=tkinter.VERTICAL) progress.pack() def start(): # 开始进度条 progress.start() def stop(): #结束 progress.stop() button = tkinter.Button(root, text='start', command=start) button.pack(side=tk.LEFT) button = tkinter.Button(root, text='stop', command=stop) button.pack(side=tk.RIGHT)
通过ttk.Style
来自定义进度条的样式,例如修改进度条的颜色、背景色。
#样式自定义进度条 style = ttk.Style() style.configure('red.Horizontal.TProgressbar', foreground='black', background='red') custom = ttk.Progressbar(root, style='red.Horizontal.TProgressbar', mode='determinate', maximum=100, value=0) custom.pack() # 更新进度条 def update(): for i in range(100): custom['value'] = i root.update() time.sleep(0.1) custom['value'] = 0 # 创建按钮 btn = tk.Button(root, text='StartStyle', command=update) btn.pack()
red.Horizontal.TProgressbar
是一个基于Progressbar
的自定义样式,用于创建一个水平方向的进度条,前景色和背景色都是红色。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。