【发布时间】:2019-11-18 16:06:58
【问题描述】:
当函数运行时,我试图在ttk.Progressbar 中显示百分比,以提醒用户执行的进程范围和剩余内容。我可以显示百分比,但百分比高达23%,这是我的tuple 的length。
如何使用相同的length 和tuple 使其达到 100%
import tkinter as tk
import tkinter.ttk as ttk
import time
tuple_1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
brt = len(tuple_1)
def progress_bar_func():
num = 0
for item in range(brt): # THIS
print(item)
num += 1 # THIS MEANS ADDING ONE TO THE PREVIOUS VALUE
progressBar['value'] = num
# THIS UPDATING TEXT IN PROGRESSBAR WITH PERCENTAGE
style.configure('text.Horizontal.TProgressbar',
text='{:g} %'.format(item))
root.update_idletasks()
time.sleep(2)
root = tk.Tk()
root.geometry("300x300")
# THIS STYLE FOR THE PROGRESSBAR
style = ttk.Style(root)
style.layout('text.Horizontal.TProgressbar',
[('Horizontal.Progressbar.trough',
{'children': [('Horizontal.Progressbar.pbar',
{'side': 'left', 'sticky': 'ns'})],
'sticky': 'nswe'}),
('Horizontal.Progressbar.label', {'sticky': ''})])
# ,lightcolor=None,bordercolo=None,darkcolor=None
style.configure('text.Horizontal.TProgressbar', text='0 %')
progressBar = ttk.Progressbar(root,style='text.Horizontal.TProgressbar', length=200, maximum=brt, value=0,)
progressBar.pack()
progress_button = tk.Button(root, text="start", command=progress_bar_func)
progress_button.pack()
root.mainloop()
【问题讨论】:
-
这只是数学 - 如果值为 23,最大值为 24,则百分比为 23/24。
-
@BryanOakley 好的,我想我可以做到
100顺便谢谢。 -
Bryan 的评论是你如何弥补
100。您需要找到您已完成的部分,即已处理的项目数与需要处理的总项目数之比。这将为您提供介于0和1之间的数字。乘以100得到百分比。 -
当它达到 24 时,24/24 = 1. 1 == 100%。