对python GUI实现完美进度条的示例详解

yipeiwu_com5年前Python基础

在用python做一个GUI界面时,想搞一个进度条实时显示下载进度,但查阅很多博客,最后的显示效果都类似下面这种:

python GUI实现进度条

这种效果在CMD界面看着还可以,但放到图形界面时就有点丑了,所以我用Canvas重新做了一个进度条,完美满足了我的要求,看着也比较舒服。

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实现进度条

以上这篇对python GUI实现完美进度条的示例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python pycharm的安装及其使用

python pycharm的安装及其使用

一.安装python 进入python官网,点击依次点击红色选中部分,开始下载。。。 下载完成后,打开安装包,如下有两个选项,一个是立即安装,另一个自定义安装,如果C盘空间足够的话,直...

python实现m3u8格式转换为mp4视频格式

python实现m3u8格式转换为mp4视频格式

开发动机:最近用手机QQ浏览器下载了一些视频,视频越来越多,占用了手机内存,于是想把下载的视频传到电脑上保存,可后来发现这些视频都是m3u8格式的,且这个格式的视频都切成了碎片,存在电脑...

完美解决Pycharm无法导入包的问题 Unresolved reference

完美解决Pycharm无法导入包的问题 Unresolved reference

如下所示: Unresolved reference 'ERROR_CODE_INPUT_ERROR' less... (Ctrl+F1) This inspection dete...

使用Python的Treq on Twisted来进行HTTP压力测试

从事API相关的工作很有挑战性,在高峰期保持系统的稳定及健壮性就是其中之一,这也是我们在Mailgun做很多压力测试的原因。 这么久以来,我们已经尝试了很多种方法,从简单的ApacheB...

pytorch-神经网络拟合曲线实例

pytorch-神经网络拟合曲线实例

代码已经调通,跑出来的效果如下: # coding=gbk import torch import matplotlib.pyplot as plt from torch.auto...