将文本格式化为 Tkinter 进度条

Formatting Text to the Tkinter Progressbar

我根据这个问题了解了如何将文本添加到进度条:

它对我有帮助,但我需要做一些更多的调整才能得到答案。

  1. 我需要在进度条左侧显示文本(百分比)。

    为此,我将 'side':'left' 添加到 Horizontal.TProgressbar.label 的布局格式中。

如下:

style.layout('text.Horizontal.TProgressbar',
    [('Horizontal.Progressbar.trough',
    {'children': [('Horizontal.Progressbar.pbar',
    {'side': 'left', 'sticky': 'ns'})],
    'sticky': 'nswe'}),
    ('Horizontal.Progressbar.label', {'side':'left','sticky': ''})])

但是,这会将文本移到最左边。它与进度条边框重叠,看不清楚。

  1. 我需要调整这段文字的字体和字体颜色。我们如何改变它?
  1. 要更改标签和边框之间的 space,您可以向布局添加填充元素

     style.layout('text.Horizontal.TProgressbar',
         [('Horizontal.Progressbar.trough',
           {'children': [('Horizontal.Progressbar.pbar',
                          {'side': 'left', 'sticky': 'ns'})],
            'sticky': 'nswe'}),
          ('Horizontal.Progressbar.padding', {'side':'left'}),
          ('Horizontal.Progressbar.label', {'side':'left','sticky': ''})])
    

    然后用

    配置padding
    style.configure('text.Horizontal.TProgressbar', padding=4)
    
  2. 您可以使用带有 fontforeground 选项的 style.configure 更改文本的字体和颜色:

     style.configure('text.Horizontal.TProgressbar', foreground="red", font='Arial 20')
    

在我对 的回答中有创建这样一个进度条的完整示例。