首页 > 解决方案 > 在 ttk 进度条中显示百分比

问题描述

我试图在ttk.Progressbar函数运行时显示百分比,以提醒用户执行的过程的范围以及剩下的内容。我可以显示百分比,但百分比是23%lengthtuple

我怎样才能使它达到 100% 相同lengthtuple

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()

进度条截图

标签: pythontkinter

解决方案


正如@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完成后截图:

进度条停止更新时的屏幕截图


推荐阅读