【问题标题】:Displaying percentage in ttk progressbar在 ttk 进度条中显示百分比
【发布时间】:2019-11-18 16:06:58
【问题描述】:

当函数运行时,我试图在ttk.Progressbar 中显示百分比,以提醒用户执行的进程范围和剩余内容。我可以显示百分比,但百分比高达23%,这是我的tuplelength

如何使用相同的lengthtuple 使其达到 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。您需要找到您已完成的部分,即已处理的项目数与需要处理的总项目数之比。这将为您提供介于01 之间的数字。乘以 100 得到百分比。
  • 当它达到 24 时,24/24 = 1. 1 == 100%。

标签: python tkinter


【解决方案1】:

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

【讨论】:

  • 使用的样式text.Horizontal.TProgressbar... 我们在哪里可以找到所有此类允许样式及其各自配置的列表?此外,我使用 bar.Hori...etc 及其背景、前景、槽色等参数。现在,当我用 text.Hori...etc 替换它时,它不再改变颜色了。它没有那些论点吗?如果我还想更改条形图的颜色并在条形图上添加测试怎么办?
猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多