【问题标题】:How to change ttk.progressBar color in python如何在 python 中更改 ttk.progressBar 颜色
【发布时间】:2012-11-10 17:51:46
【问题描述】:

有人知道如何更改 ttk.progressBar 的颜色吗?它现在显示为绿色,我希望它是蓝色的。

import ttk
self.progressBar = ttk.Progressbar(frame3, length=560, maximum=100, mode='determinate');
self.progressBar.place(x=-5, y=60)

【问题讨论】:

  • 这是 my question 的副本,但希望这会引起更多关注
  • 但是,基本上,您只能更改 Windows 主题的一个(alt)中的颜色。但是使用 tcl 你可以改变状态,但我不确定...

标签: python colors progress-bar ttk


【解决方案1】:

Progressbarappears to take 一个样式参数。根据documentation,可以使用样式设置前景色和背景色。

注意:我自己没有试过,只是给你指点相关文档

【讨论】:

    【解决方案2】:

    您可以更改进度条的颜色,但这很棘手。首先,你需要明白,如果你使用默认主题,如果你没有在tk.style中指定主题,就是默认主题。然后它将所需的所有信息传递给操作系统,操作系统将使用其样式进行绘图,而忽略您传递的样式信息。这意味着它将在 Windows 上绘制一个 Window 风格的绿色进度条,依此类推。您需要做的是将主题更改为 ttk 绘制的自定义主题。试试"clam" 样式,它是 ttk 允许您选择的最好看的样式之一。这是我写的一个脚本的改编摘录:

    import Tkinter as tk
    import ttk as ttk
    root = tk.Tk()
    frame = tk.Frame(root)
    frame.grid()
    s = ttk.Style()
    s.theme_use('clam')
    s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
    ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal",
                    length=600, mode="determinate", maximum=4, value=1).grid(row=1, column=1)
    frame.pack()
    

    这是一张确认它有效的图片。

    【讨论】:

    • 这种方式不是pythonic。考虑恢复旧样式。只是改变颜色是如此复杂。 Tkinter 应该添加一个 api 来更改进度条的状态(正常、暂停、错误),就像在 win32 中一样:docs.microsoft.com/zh-cn/windows/win32/controls/pbm-setstate
    • 如果我删除了theme_use,颜色的变化不起作用,事实上这个过程没有显示任何效果,因为你添加的图片中的输出也会产生使用您提到的主题。
    【解决方案3】:

    我发现它对我不起作用, 我不想说这是错误的 我已经发现 使用 [ troughcolor ] 更改背景和 [ background ] 指示栏

    不适合我 s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')

    我的工作方式 s.configure("red.Horizontal.TProgressbar", troughcolor ='gray', background='red')

    【讨论】:

    • 您的回答对我也有帮助。感谢您提供此解决方案。
    【解决方案4】:

    您需要使用样式来设置进度条颜色。进度条样式元素有:

    Element Defines
    troughcolor background of full widget
    background background of progress bar
    darkcolor bottom & right progress bar innermost border
    lightcolor top & left bar progress bar innermost border
    bordercolor outline border of the progress bar (outside the above border) and the whole widget

    如果您希望边框与进度条颜色相同,可以使用以下代码:

    TROUGH_COLOR = 'blue'
    BAR_COLOR = 'green'
    style.configure("bar.Horizontal.TProgressbar", troughcolor=TROUGH_COLOR, 
                    bordercolor=TROUGH_COLOR, background=BAR_COLOR, lightcolor=BAR_COLOR, 
                    darkcolor=BAR_COLOR)
    

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 2012-08-05
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      相关资源
      最近更新 更多