Python tkinter 进度条代码

 1 import tkinter as tk
 2 import time
 3 
 4 # 创建主窗口
 5 window = tk.Tk()
 6 window.title('进度条')
 7 window.geometry('630x150')
 8 
 9 # 设置下载进度条
10 tk.Label(window, text='下载进度:', ).place(x=50, y=60)
11 canvas = tk.Canvas(window, width=465, height=22, bg="white")
12 canvas.place(x=110, y=60)
13 
14 
15 # 显示下载进度
16 def progress():
17     # 填充进度条
18     fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
19     x = 500  # 未知变量,可更改
20     n = 465 / x  # 465是矩形填充满的次数
21     for i in range(x):
22         n = n + 465 / x
23         canvas.coords(fill_line, (0, 0, n, 60))
24         window.update()
25         time.sleep(0.02)  # 控制进度条流动的速度
26 
27     # 清空进度条
28     fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
29     x = 500  # 未知变量,可更改
30     n = 465 / x  # 465是矩形填充满的次数
31 
32     for t in range(x):
33         n = n + 465 / x
34         # 以矩形的长度作为变量值更新
35         canvas.coords(fill_line, (0, 0, n, 60))
36         window.update()
37         time.sleep(0)  # 时间为0,即飞速清空进度条
38 
39 
40 btn_download = tk.Button(window, text='启动进度条', command=progress)
41 btn_download.place(x=400, y=105)
42 
43 window.mainloop()
原文地址:https://www.cnblogs.com/lize19940412/p/14780881.html