ttk.Progressbar不会更改布局

时间:2018-02-15 21:25:46

标签: python tkinter progress-bar ttk

我想稍后在我的音乐播放器应用中使用ttk.Progressbar,但我想先改变外观。我已经在网上搜索答案,但没有用。

如果我使用theme_use("default"),以下代码效果很好,但我无法使用它,因为它会更改ttk.Treeview的样式。对于ttk.Treeview我已经创建了element_create("Custom.Treeheading.border", "from", "default")layout

我决定制作另一种服装布局,但它不会改变小部件的外观。

以下是代码:

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()

但是,我正在靠墙打击以找出解决方案。

1 个答案:

答案 0 :(得分:0)

如果您只想更改其选项,则不需要创建新元素,在您的情况下只需配置" Niklas.Horizo​​ntal.TProgressbar"式:

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()

我在您的代码中发现的一个问题是,您为进度条指定了样式" Niklas.Horizo​​ntal.TProgressbar.trough",这不是进度条样式,而是样式的元素,因此进度条确实如此不行。