Home > Software engineering >  How to display text in two different Progressbars with tkinter?
How to display text in two different Progressbars with tkinter?

Time:10-10

I have a window with two different Progressbars. I want to add different text information over them (i.e. "88/1524" on the first one and "88/10000" on the second). I've found a solution for one Progressbar on this question: Progressbars

Where I'm stuck at: enter image description here

And portion of my code:

style = tkinter.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/0')

ProgressBarOfDirectory = tkinter.ttk.Progressbar(ProgressbarFrame,
                                                 style='text.Horizontal.TProgressbar',
                                                 orient="horizontal",
                                                 length=WidthOfElements,
                                                 mode="determinate",
                                                 maximum=NbStepOfProgressBar,
                                                 variable=PourcentageOfDirectoryAnalysisDone)
ProgressBarOfDirectory.pack(side="top")

ProgressBarOfAll = tkinter.ttk.Progressbar(ProgressbarFrame,
                                           style='text.Horizontal.TProgressbar',
                                           orient="horizontal",
                                           length=WidthOfElements,
                                           mode="determinate",
                                           maximum=NbStepOfProgressBar,
                                           variable=PourcentageOfAllAnalysisDone)

CodePudding user response:

The progress bar doesn’t have a feature to display text on its own.

What you could do is to use a Label widget. All you have to do is to place a Label widget where ever you want to implement the text.

Now you can configure the text value of the label & can change it with the change in the Python Tkinter progress bar.

Here you can find an example:

from tkinter import *
from tkinter.ttk import Progressbar
import time


def step():
    for i in range(5):
        ws.update_idletasks()
        pb['value']  = 20
        time.sleep(1)
        txt['text']=pb['value'],'%'

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x150')
ws.config(bg='#345')


pb = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 100,
    mode = 'determinate'
    )

pb.place(x=40, y=20)

txt = Label(
    ws,
    text = '0%',
    bg = '#345',
    fg = '#fff'

)

txt.place(x=150 ,y=20 )

Button(
    ws,
    text='Start',
    command=step
).place(x=40, y=50)

ws.mainloop()

CodePudding user response:

Widget styles aren't shared unless the same one it specified for more than one widget (or the same default one gets assigned to more than one because something else wasn't specified).

To take advantage of that here's an updated version of the code to my screenshots of it running

  • Related