Skip to content Skip to sidebar Skip to footer

Progressbar In Tkinter With A Label Inside

Is It possible to improve my progressbar in Tkinter-Python adding a label in the middle (ex: reading file)? I tried to find a elegant coding solution but without a real result fr

Solution 1:

I added the label inside the progressbar by creating a custom ttk style layout. The text of the label is changed by configuring the style:

from tkinter import Tk
from tkinter.ttk import Progressbar, Style, Button
from time import sleep


root = Tk()
s = Style(root)
# add the label to the progressbar style
s.layout("LabeledProgressbar",
         [('LabeledProgressbar.trough',
           {'children': [('LabeledProgressbar.pbar',
                          {'side': 'left', 'sticky': 'ns'}),
                         ("LabeledProgressbar.label",   # label inside the bar
                          {"sticky": ""})],
           'sticky': 'nswe'})])

p = Progressbar(root, orient="horizontal", length=300,
                style="LabeledProgressbar")
p.pack()

# change the text of the progressbar, # the trailing spaces are here to properly center the text
s.configure("LabeledProgressbar", text="0 %      ")

deffct():
    for i inrange(1, 101):
        sleep(0.1)
        p.step()
        s.configure("LabeledProgressbar", text="{0} %      ".format(i))
        root.update()

Button(root, command=fct, text="launch").pack()

root.mainloop()

screenshot

Post a Comment for "Progressbar In Tkinter With A Label Inside"