在本文中,将介绍 Tkinter Progressbar 进度条小部件。Progressbar 进度条小部件向用户提供长时间运行任务时的进度反馈。
要创建 Progressbar 进度条小部件,请使用以下构造函数:
ttk.Progressbar(container, orient, length, mode)
主要参数如下:
进度条模式
determinate 模式:
进度条会从起点延伸至终点,当知道任务所需时间时,可以使用此模式,这是默认确定模式。
import tkinter as tk
from tkinter import ttk
import time
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Progressbar 进度条演示')
pb = ttk.Progressbar(root, length=280)
pb.pack(expand=True)
frame=ttk.Frame(root)
start_button = ttk.Button(root, text='开始', command=pb.start)
start_button.pack(side=tk.LEFT, expand=True)
stop_button = ttk.Button(root, text='结束', command=pb.stop)
stop_button.pack(side=tk.LEFT, expand=True)
frame.pack(fill=tk.X, expand=True)
root.mainloop()
indeterminate 模式:
一段进度条会在起点和终点间来回移动,当不知道任务所需时间时,可以使用此不确定模式。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Progressbar 进度条演示')
pb = ttk.Progressbar(root, orient='horizontal', mode='indeterminate', length=280)
pb.pack(expand=True)
frame=ttk.Frame(root)
start_button = ttk.Button(root, text='开始', command=pb.start)
start_button.pack(side=tk.LEFT, expand=True)
stop_button = ttk.Button(root, text='结束', command=pb.stop)
stop_button.pack(side=tk.LEFT, expand=True)
frame.pack(fill=tk.X, expand=True)
root.mainloop()
进度条的值
可以使用参数variable 结合set() 方法,或者使用参数value 设置进度条的值。使用cget() 方法获取进度条的值。
import tkinter as tk
from tkinter import ttk
import time
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Progressbar 进度条演示')
var=tk.IntVar()
def start1():
for i in range(1, 101):
var.set(i)
label1['text']=str(pb1.cget('value')),'%'
root.update() # 刷新窗口
time.sleep(0.05)
def start2():
for j in range(100):
pb2['value'] = j + 1
label2['text']=str(pb2.cget('value')),'%'
root.update() # 刷新窗口
time.sleep(0.05)
pb1 = ttk.Progressbar(root, orient='horizontal', length=280, variable=var)
pb1.pack(expand=True)
label1 = tk.Label(root,text='0%')
label1.pack()
pb2 = ttk.Progressbar(root, orient='horizontal', length=280)
pb2.pack(expand=True)
label2 = tk.Label(root,text='0%')
label2.pack()
frame=ttk.Frame(root)
start_button = ttk.Button(root, text='开始1', command=start1)
start_button.pack(side=tk.LEFT, expand=True)
stop_button = ttk.Button(root, text='开始2', command=start2)
stop_button.pack(side=tk.LEFT, expand=True)
frame.pack(fill=tk.X, expand=True)
root.mainloop()
进度条的样式
利用 Ttk 主题小部件的特性,自定义进度条的样式。
import tkinter as tk
from tkinter import ttk
import time
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Progressbar 进度条演示')
var=tk.IntVar()
style = ttk.Style()
style.theme_use("clam")
style.configure("design.Horizontal.TProgressbar",
background="lightblue",
troughcolor="lightgray",
bordercolor="darkblue",
lightcolor="lightblue",
darkcolor="darkblue")
def start():
pb2.start()
for i in range(100):
var.set(i)
label1['text']=str(pb1.cget('value')) + '%'
root.update() # 刷新窗口
time.sleep(0.05)
pb1 = ttk.Progressbar(root, orient='horizontal', length=280, variable=var, style='design.Horizontal.TProgressbar')
pb1.pack(expand=True)
label1 = tk.Label(root,text='0%')
label1.pack()
pb2 = ttk.Progressbar(root, orient='horizontal', mode='indeterminate', length=280, style='design.Horizontal.TProgressbar')
pb2.pack(expand=True)
label2 = tk.Label(root,text='0%')
label2.pack()
frame=ttk.Frame(root)
start_button = ttk.Button(root, text='开始', command=start)
start_button.pack(side=tk.LEFT, expand=True)
frame.pack(fill=tk.X, expand=True)
root.mainloop()
进度条的方法
start() 方法:
启动进度条的移动,设置移动的速度,以毫秒为单位。
step() 方法:
控制进度条的步长。每次调用该方法,进度条都会向前移动指定的长度,默认 1。
stop() 方法:
进度条停止,结束移动。
import tkinter as tk
from tkinter import ttk
import time
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Progressbar 进度条演示')
root.grid_anchor('center')
def startall():
pb1.start(5)
pb2.start(10)
pb3.start(5)
pb4.start(10)
def stepall():
pb1.step(10)
pb2.step(8)
pb3.step(25)
pb4.step(15)
def stopall():
pb1.stop()
pb2.stop()
pb3.stop()
pb4.stop()
pb1 = ttk.Progressbar(root, orient='horizontal', length=280)
pb1.grid(row=0,column=0,pady=10,padx=10)
pb2 = ttk.Progressbar(root, orient='horizontal', mode='indeterminate', length=280)
pb2.grid(row=1,column=0,pady=10,padx=10)
pb3 = ttk.Progressbar(root, orient='vertical', length=280)
pb3.grid(row=0,column=1,pady=10,padx=10,rowspan=2)
pb4 = ttk.Progressbar(root, orient='vertical', mode='indeterminate', length=280)
pb4.grid(row=0,column=2,pady=10,padx=10,rowspan=2)
start_button = ttk.Button(root, text='开始', command=startall)
start_button.grid(row=2,column=0,pady=10,padx=10)
start_button = ttk.Button(root, text='步进', command=stepall)
start_button.grid(row=2,column=1,pady=10,padx=10)
start_button = ttk.Button(root, text='停止', command=stopall)
start_button.grid(row=2,column=2,pady=10,padx=10)
root.mainloop()
文件下载进度示例
以下代码将下载 Python 到 C 盘。
import tkinter as tk
from tkinter import ttk
from threading import Thread
from urllib.request import urlretrieve, urlcleanup
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Progressbar 进度条演示')
root.grid_anchor('center')
def download():
url = "https://mirrors.huaweicloud.com/python/3.13.1/python-3.13.1-amd64.exe"
urlretrieve(url, "c:\python-3.13.1-amd64.exe", download_status)
urlcleanup()
def download_button_clicked():
Thread(target=download).start()
def download_status(count, data_size, total_data):
if count == 0:
progressbar.configure(maximum=total_data)
else:
progressbar.step(data_size)
label['text']=f"{progressbar.cget('value')/total_data*100:.2f}%"
download_button = ttk.Button(text="下载 Python", command=download_button_clicked)
download_button.grid(row=0,column=0,pady=10,padx=10)
progressbar = ttk.Progressbar(root, orient='horizontal', length=280)
progressbar.grid(row=1,column=0,pady=10,padx=10)
label = ttk.Label(root,text='0%')
label.grid(row=1,column=1,pady=10,padx=10)
root.mainloop()
原创 信息技术资源 信息科技云课堂 2024年12月23日 08:25 山东
标签:tkinter,进度条,Python,button,ttk,start,tk,root From: https://www.cnblogs.com/o-O-oO/p/18626945