我正在编写 TKinter GUI 应用程序,并且遇到进度条样式配置问题。 我在不同的机器上运行相同的代码,一次可以,而另一次则不行。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
app = tk.Frame(root)
app.pack()
app.green_style = ttk.Style()
app.green_style.configure("myStyle.Horizontal.TProgressbar", background='yellow',foreground = 'blue')
app.progress = ttk.Progressbar(root, length=200, mode='determinate',style="myStyle.Horizontal.TProgressbar")
app.progress.pack()
app.progress['value'] = 75
app.mainloop()
在 Linux 和 python 3.7.3 中,它会创建一个黄色进度条:
但是在 Windows 10 和 Python 3.10.11 中,样式不会改变:
您能帮我理解为什么 Windows 进度条样式没有更新吗?
谢谢!
摘自官方文档:
某些选项仅适用于特定主题。
所以尝试另一个主题,比如“winnative”:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
app = tk.Frame(root)
app.pack()
app.green_style = ttk.Style()
app.green_style.theme_use("winnative") # use another theme
app.green_style.configure("myStyle.Horizontal.TProgressbar",
background="yellow", foreground="blue")
app.progress = ttk.Progressbar(root, length=200, mode="determinate",
style="myStyle.Horizontal.TProgressbar")
app.progress.pack(expand=1, padx=10, pady=10)
app.progress["value"] = 75
app.mainloop()
结果: