如何在Tkinter进度栏中更改颜色

时间:2019-02-05 10:38:14

标签: python python-3.x tkinter progress-bar

我在Python 3中使用了tkiner mesagebox和ttk进度栏。我尝试在一行中设置一个文本窗口,在下一行中设置一个进度栏。到目前为止,此方法一直有效,但是我无法将颜色从绿色(默认)更改为另一个值。通过这篇帖子How to change ttk.progressBar color in python,我能够将颜色变成黑色,但是后来我不知道如何获得文本。有人可以帮我吗?

from tkinter import *
from tkinter import messagebox
from tkinter import ttk

#bar in green with text
root = Tk()
gpw_l1 = Label(root, text="This should be a black bar")
gpw_l2 = ttk.Progressbar(root, orient="horizontal", length=500, mode="determinate")
gpw_l2.grid(row=2, column=0, pady=10)
gpw_l2["maximum"] = 1.0
x = 0.7
gpw_l2["value"] = x

gpw_l1.grid(row=0, columnspan=2)
gpw_l2.grid(row=1, columnspan=2)
root.geometry('+100+200')
root.mainloop()
root.quit()

#bar in red, but no text
root2 = Tk()
frame = Frame(root2)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='black')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600, mode="determinate",
                maximum=4, value=1).grid(row=1, column=1)
frame.pack()
root2.mainloop()
root2.quit()

1 个答案:

答案 0 :(得分:1)

from tkinter import *
from tkinter import messagebox
from tkinter import ttk


#bar in red, but no text
root2 = Tk()
frame = Frame(root2)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='black')
gpw_l1 = Label(frame, text="This should be a black bar").grid(row=1, column=1)
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600, mode="determinate",
                maximum=4, value=1).grid(row=2, column=1)
frame.pack()
root2.mainloop()
root2.quit()