How to change some settings on LabeledProgressbar in tkinter?

Question:

The source code is ..

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


def prog():
    for i in range(0, 11):
        time.sleep(0.1)
        p1.step(100 / 10)
        txt = str(i) + '/' + str(10)
        s1.configure("LabeledProgressbar", text=txt)
        fn.update()

    for i in range(0, 16):
        time.sleep(0.1)
        p2.step(100 / 15)
        txt = str(i) + '/' + str(15)
        # print(txt)
        s2.configure("LabeledProgressbar", text=txt)
        fn.update()


fn = Tk()
fn.geometry("500x300+300+25")

b1 = Button(fn, text='progression', width=50, command=prog)
b1.place(x=75, y=150)

s1 = Style(fn)
s1.layout("LabeledProgressbar",
          [('LabeledProgressbar.trough',
            {'children': [('LabeledProgressbar.pbar',
                           {'side': 'left', 'sticky': 'ns'}),
                          ("LabeledProgressbar.label",
                           {"sticky": ""})],
             'sticky': 'nswe'})])
p1 = Progressbar(fn, orient="horizontal", length=300, style="LabeledProgressbar")
p1.place(x=100, y=10)

s2 = Style(fn)
s2.layout("LabeledProgressbar",
          [('LabeledProgressbar.trough',
            {'children': [('LabeledProgressbar.pbar',
                           {'side': 'left', 'sticky': 'ns'}),
                          ("LabeledProgressbar.label",
                           {"sticky": ""})],
             'sticky': 'nswe'})])
p2 = Progressbar(fn, orient="horizontal", length=300, style="LabeledProgressbar")
p2.place(x=100, y=90)

fn.mainloop()

but output of progress bar shows on the first attached file.. The progress is like "enter image description hereembossed".

I want the output like the second image. enter image description here
Your help is much appreciated. Thank you in advance

Asked By: fardV

||

Answers:

First you don’t have to repeat the layout in your code: just define the list once and use it where you need it. Then you can define configure style options with s.configure(). Same thing you can define the options in a dictionary and use them when needed (Note that if you want different style options for both bars, you’d have to name the layout instances differently):

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


def prog():
    for i in range(0, 11):
        time.sleep(0.1)
        p1.step(100 / 10)
        txt = str(i) + '/' + str(10)
        s1.configure("LabeledProgressbar", text=txt)
        fn.update()

    for i in range(0, 16):
        time.sleep(0.1)
        p2.step(100 / 15)
        txt = str(i) + '/' + str(15)
        # print(txt)
        s2.configure("LabeledProgressbar", text=txt)
        fn.update()

labeled_progressbar_layout = [('LabeledProgressbar.trough',
                                {'children': [('LabeledProgressbar.pbar',
                                            {'side': 'left', 'sticky': 'ns'}),
                                            ("LabeledProgressbar.label",
                                            {"sticky": ""})],
                                'sticky': 'nswe'})]

label_progressbar_style = dict(background="blue",       # add your style options here
                            borderwidth=0,
                            font=('Helvetica', 8, 'bold'),
                            ) 

fn = Tk()
fn.geometry("500x300+300+25")

b1 = Button(fn, text='progression', width=50, command=prog)
b1.place(x=75, y=150)

s1 = Style(fn)
s1.layout("LabeledProgressbar", labeled_progressbar_layout)
s1.configure('LabeledProgressbar', **label_progressbar_style)

p1 = Progressbar(fn, orient="horizontal", length=300, style="LabeledProgressbar")
p1.place(x=100, y=10)

s2 = Style(fn)
s2.layout("LabeledProgressbar", labeled_progressbar_layout)
p2 = Progressbar(fn, orient="horizontal", length=300, style="LabeledProgressbar")
p2.place(x=100, y=90)

fn.mainloop()

Output:

enter image description here

Edit: adding borderwidth=0 to the style options so that the bar isn’t embossed. See tcl doc for general options or progressbar specific

Answered By: Tranbi