python - 在 ttk 进度条中显示百分比
问题描述
我试图在ttk.Progressbar
函数运行时显示百分比,以提醒用户执行的过程的范围以及剩下的内容。我可以显示百分比,但百分比是23%
我length
的tuple
。
我怎样才能使它达到 100% 相同length
的tuple
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()
解决方案
正如@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
完成后截图:
推荐阅读
- r - 在 R 中使用 group_by() 中的参数产生 function() 的问题
- xml - Powershell如何检查xml对象中的文本内容是否为空
- autosuggest - 如何影响 Here Maps 的自动建议结果
- java - 错误:尝试在空对象引用上调用虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String)”
- python - 遍历 url 列表并获取选定的文本
- pyspark - 如何在 Pyspark 函数中引用包含 f 字符串的列?
- jdbc - 如何将 Apache Zeppelin JDBC 解释器限制为仅选择查询
- c++ - 错误:我的 C++ 程序中损坏的大小与 prev_size
- powershell - 如何使用外部文件(如 .txt)中的阻止列表在 Powershell 管道中进行过滤
- ios - 与模拟器相比,dispatchgroup 在 testflight 中以不同的顺序执行任务