You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不使用内置主题修改ttk.Progressbar进度条颜色的问题咨询

解决方案

核心原因

ttk默认使用的系统原生主题(Windows下为win32、macOS下为aqua)的进度条由系统原生接口绘制,直接修改样式的background、foreground属性不会生效。全局切换主题会影响所有ttk组件,因此我们可以仅给自定义的进度条样式指定非原生绘制规则,完全不影响其他组件的样式。

修改方法

不需要全局调用theme_use,只需要给你定义的5种进度条样式复用内置clam主题的进度条绘制布局即可,修改后的完整代码如下:

import tkinter as tk
import tkinter.ttk as ttk

class ProgressBarPercent(tk.Frame):
    def __init__(self, parent, orient, length, mode):
        tk.Frame.__init__(self, parent)
        self.root = parent

        # ttk style
        self.style = ttk.Style()
        # 核心修改:仅给自定义进度条复用clam主题的绘制逻辑,不影响全局其他组件
        clam_pbar_layout = self.style.layout("clam.Horizontal.TProgressbar")
        # 给每个自定义样式指定绘制布局
        self.style.layout("green.Horizontal.TProgressbar", clam_pbar_layout)
        self.style.layout("blue.Horizontal.TProgressbar", clam_pbar_layout)
        self.style.layout("yellow.Horizontal.TProgressbar", clam_pbar_layout)
        self.style.layout("orange.Horizontal.TProgressbar", clam_pbar_layout)
        self.style.layout("red.Horizontal.TProgressbar", clam_pbar_layout)
        
        # 配置各样式颜色,可额外添加troughcolor参数修改进度条槽的颜色
        self.style.configure("green.Horizontal.TProgressbar", background="green")
        self.style.configure("blue.Horizontal.TProgressbar", background="blue")
        self.style.configure("yellow.Horizontal.TProgressbar", background="yellow")
        self.style.configure("orange.Horizontal.TProgressbar", background="orange")
        self.style.configure("red.Horizontal.TProgressbar", background="red")
        
        # Progressbar, percent label
        self.progress = ttk.Progressbar(self, orient = orient, 
              length = length, mode = mode, style = "orange.Horizontal.TProgressbar")
        self.text = tk.StringVar()
        self.text.set("0%")
        self.percent = tk.Label(self, textvariable = self.text)
        self.percent.grid(column=0, row=0)
        self.progress.grid(column=1, row=0)

    def value(self, value):
        # Progressbar updates based on input value
        self.progress['value'] = value
        percent_value = round(value, 1)
        self.text.set(str(percent_value) + '%')
        alert = False
        if value < 25:
            self.progress.configure(style = "green.Horizontal.TProgressbar")
        elif 25 <= value < 50:            
            self.progress.configure(style = "blue.Horizontal.TProgressbar")
        elif 50 <= value < 75:            
            self.progress.configure(style = "yellow.Horizontal.TProgressbar")
        elif 75 <= value <= 100:            
            self.progress.configure(style = "orange.Horizontal.TProgressbar")
        elif 100 <= value:
            alert = True
            self.progress.configure(style = "red.Horizontal.TProgressbar")
        self.alert(alert)

    def alert(self, alert):
        if alert == True:
            self.percent.configure(fg = 'red')
        else:
            self.percent.configure(fg = 'black')


if __name__ == "__main__":
    root = tk.Tk()
    # 可以在这里加一个其他ttk组件测试样式不受影响,比如ttk.Button
    ttk.Button(root, text="测试其他组件样式不变").grid(pady=5)
    progress = ProgressBarPercent(root, orient = tk.HORIZONTAL, 
                  length = 100, mode = 'determinate') 

    progress.grid()
    progress.value(67.322)

    root.mainloop()

补充说明

  • 该方案跨平台通用,clam是tkinter内置自带主题,所有运行环境都支持
  • 若需要修改进度条凹槽的颜色,可在configure参数中添加troughcolor="你要的颜色"即可
  • 所有其他ttk组件仍会使用系统默认主题样式,不会被改动

内容的提问来源于stack exchange,提问作者jezza_99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 16:54:09