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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM