Formatting Text to the Tkinter Progressbar

Viewed 173

I got to know about adding the text to the progress bar as per this question:

Displaying percentage in ttk progressbar

It helps me but I need a few more adjustments to do to the answer.

  1. I need the text (the percentage) to appear on the left side of the progressbar.

    For this, I added 'side':'left' to the layout format for Horizontal.TProgressbar.label.

As under:

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

However, this moves the text to extreme left. It overlaps the progressbar border and is not clearly readable.

  1. I need to adjust the font and font color of this text. How do we change that?
1 Answers
  1. To change the space between the label and the border you can add a padding element to the layout

     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': ''})])
    

    Then configure the padding with

    style.configure('text.Horizontal.TProgressbar', padding=4)
    
  2. You can change the font and color of the text using style.configure with the font and foreground options:

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

There is a full example of creating such a progressbar in my answer to Progressbar with Percentage Label?.

Related