Search code examples
pythontkinterprogress-barttk

ttk.Progressbar does not change the Layout


I want to use the ttk.Progressbar in my musicplayer app later, but I want to change the appearance first. I have scoured the net for an answer but no avail.

The following code works well if I use the theme_use("default"), but I can't use it because it will change the style of my ttk.Treeview. For the ttk.Treeview I already created an element_create("Custom.Treeheading.border", "from", "default") and a layout.

I decided to make another costume layout, but it wont change the appearance of the widget.

Here is the code:

import tkinter
from tkinter import ttk


def stoppb():
    pb.stop()

def startpb():
    pb.start(100)

master = tkinter.Tk()
master.minsize(width=500, height=300)
master.configure(bg="#2c2c2d")

s = ttk.Style()

#I can't use a theme because I already use create_element
#s.theme_use("default")

#I used this to figure out elements of the widget and its options
t = ttk.Progressbar(None)
tClass = t.winfo_class()
print("tClass", tClass)

layout = s.layout("Horizontal.TProgressbar")
print("layout", layout)

d = s.element_options("Horizontal.TProgressbar.trough")
e = s.element_options("Horizontal.TProgressbar")
print("element_options: Horizontal.TProgressbar.trough", d)
print("element_options: Horizontal.TProgressbar", e)

s.element_create("Niklas.Horizontal.TProgressbar.trough", "from", "default"
s.layout("Niklas.Horizontal.TProgressbar.trough", [
    ("Horizontal.Progressbar.trough", {"sticky": "nswe", "children": [
        ("Horizontal.Progressbar.pbar", {"side": "left", "sticky": "ns"})
    ]}),
])

s.configure("Niklas.Horizontal.TProgressbar.trough",
        troughcolor="#121212",
        background="#b2b2b2",
        thickness=1,
        troughrelief="flat",
        relief="flat",
        borderwidth=0)

pb = ttk.Progressbar(master,
                 style="Niklas.Horizontal.TProgressbar.trough",
                 orient="horizontal",
                 mode="determinate",
                 maximum=230)
pb.place(x=100, y=50)

tkinter.Button(master, text="Stop", command=stoppb).place(x=100, y=140)
tkinter.Button(master, text="Start", command=startpb).place(x=140, y=140)

master.mainloop()

But I am beating my head against the wall to figure out a solution.


Solution

  • You don't need to create a new element if you only want to change its options, in your case just configure the "Niklas.Horizontal.TProgressbar" style:

    import tkinter
    from tkinter import ttk
    
    master = tkinter.Tk()
    
    s = ttk.Style()
    
    s.configure("Niklas.Horizontal.TProgressbar",
                troughcolor="#121212",
                background="#b2b2b2",
                thickness=1,
                troughrelief="flat",
                relief="flat",
                borderwidth=0)
    
    pb = ttk.Progressbar(master,
                         style="Niklas.Horizontal.TProgressbar",
                         orient="horizontal",
                         mode="determinate",
                         maximum=230)
    pb.grid(columnspan=2, sticky='ew')
    pb.start()
    
    master.mainloop()
    

    An issue I identified in your code is that you gave the progressbar the style "Niklas.Horizontal.TProgressbar.trough", which is not a progressbar style but an element of the style, so the progressbar did not work.