gpt4 book ai didi

python - 在 ttk 进度条中显示百分比

转载 作者:行者123 更新时间:2023-12-04 15:38:30 28 4
gpt4 key购买 nike

我正在尝试在函数运行时在 ttk.Progressbar 中显示百分比,以提醒用户已执行的进程的范围和剩余的进程。能够正确显示百分比,但百分比最高为 23%,这是我的 元组长度

如何使 tuplelength 达到 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()

screenshot of progressbar

最佳答案

正如@Bryan Oakley 和其他人指出的那样,解决方案主要是正确地进行数学计算。这是根据您问题中的代码执行此操作的具体示例。我还对其进行了修改,以演示 Progressbar 以我认为稍微好一点的方式工作——即使用通用的 after()。函数定期更新柱的值。

import time
import tkinter as tk
import tkinter.ttk as ttk

tuple_1 = tuple(range(1, 25))

def progress_bar_func(style, progress_bar, sequence):
root.after(500, update_progress_bar, style, progress_bar, 1, len(sequence))

def update_progress_bar(style, progress_bar, num, limit):
if num <= limit:
percentage = round(num/limit * 100) # Calculate percentage.
progress_bar.config(value=num)
style.configure('text.Horizontal.TProgressbar', text='{:g} %'.format(percentage))
num += 1
root.after(500, update_progress_bar, style, progress_bar, num, limit)

root = tk.Tk()
root.geometry("300x300")

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': ''})])
style.configure('text.Horizontal.TProgressbar', text='0 %')

progress_bar = ttk.Progressbar(root, style='text.Horizontal.TProgressbar', length=200,
maximum=len(tuple_1), value=0)
progress_bar.pack()

progress_button = tk.Button(root, text="start",
command=lambda: progress_bar_func(style, progress_bar, tuple_1))
progress_button.pack()

root.mainloop()

Progressbar 完成后的截图:

screenshot of progressbar when it stops updating

关于python - 在 ttk 进度条中显示百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918648/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com