在用python做一个gui界面时,想搞一个进度条实时显示下载进度,但查阅很多博客,最后的显示效果都类似下面这种:
这种效果在cmd界面看着还可以,但放到图形界面时就有点丑了,所以我用canvas重新做了一个进度条,完美满足了我的要求,看着也比较舒服。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import time import threading from tkinter import * def update_progress_bar(): for percent in range ( 1 , 101 ): hour = int (percent / 3600 ) minute = int (percent / 60 ) - hour * 60 second = percent % 60 green_length = int (sum_length * percent / 100 ) canvas_progress_bar.coords(canvas_shape, ( 0 , 0 , green_length, 25 )) canvas_progress_bar.itemconfig(canvas_text, text = '%02d:%02d:%02d' % (hour, minute, second)) var_progress_bar_percent. set ( '%0.2f %%' % percent) time.sleep( 1 ) def run(): th = threading.thread(target = update_progress_bar) th.setdaemon(true) th.start() top = tk() top.title( 'progress bar' ) top.geometry( '800x500+290+100' ) top.resizable(false, false) top.config(bg = '#535353' ) # 进度条 sum_length = 630 canvas_progress_bar = canvas(top, width = sum_length, height = 20 ) canvas_shape = canvas_progress_bar.create_rectangle( 0 , 0 , 0 , 25 , fill = 'green' ) canvas_text = canvas_progress_bar.create_text( 292 , 4 , anchor = nw) canvas_progress_bar.itemconfig(canvas_text, text = '00:00:00' ) var_progress_bar_percent = stringvar() var_progress_bar_percent. set ( '00.00 %' ) label_progress_bar_percent = label(top, textvariable = var_progress_bar_percent, fg = '#f5f5f5' , bg = '#535353' ) canvas_progress_bar.place(relx = 0.45 , rely = 0.4 , anchor = center) label_progress_bar_percent.place(relx = 0.89 , rely = 0.4 , anchor = center) # 按钮 button_start = button(top, text = '开始' , fg = '#f5f5f5' , bg = '#7a7a7a' , command = run, height = 1 , width = 15 , relief = groove, bd = 2 , activebackground = '#f5f5f5' , activeforeground = '#535353' ) button_start.place(relx = 0.45 , rely = 0.5 , anchor = center) top.mainloop() |
显示效果如下:
以上这篇对python gui实现完美进度条的示例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/polyhedronx/article/details/82291189