更改 ttk 小部件文本颜色

2023-09-03Python开发问题
52

本文介绍了更改 ttk 小部件文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经到处搜索了,但还没有找到一个简单的示例来展示如何更改 ttk 小部件样式的次要元素并动态应用它(在小部件创建之后).

I've searched all over, but have yet to find a simple example showing how to change a minor element of a ttk widget style and applying it dynamically (after widget creation).

我有一些代表一些配置项的 ttk 检查按钮,以及一个用于更新远程系统的 ttk 按钮.检查按钮被初始化为远程系统的状态.

I have some ttk checkbuttons representing some configuration items, and a ttk button used to update a remote system. The checkbuttons are initialized to the state of the remote system.

如果用户修改了任何检查按钮,我希望检查按钮文本和更新按钮文本都变为红色,以提醒用户检查按钮状态不再与远程状态匹配,并且应该按下更新按钮将修改后的配置发送到远程系统.

If the user modifies any of the checkbuttons, I want both the checkbutton text and the update button text to become red, to remind the user that the checkbutton state no longer matches the remote state, and that the update button should be pressed to send the modified configuration to the remote system.

当按下更新按钮时,更新按钮和所有复选按钮的文本颜色恢复为黑色.

When the update button is pressed, the text color of the update button and all checkbuttons reverts to black.

我知道这是可能的(而且可能很容易),但是怎么做呢?

I know this is possible (and is probably easy), but how?

修改背景颜色也可以.

推荐答案

您需要创建一个自定义样式,然后将该样式应用到小部件.要创建自定义样式,首先获取 ttk.Style 的实例,然后使用 configure 方法从现有样式派生新样式.下面的示例创建一个名为Red.TCheckbutton"的新样式:

You will need to create a custom style, and then apply that style to the widget. To create a custom style, first get an instance of ttk.Style, and then use the configure method to derive a new style from an existing one. The following example creates a new style named "Red.TCheckbutton":

style = ttk.Style()
style.configure("Red.TCheckbutton", foreground="red")

接下来,当您想要改变颜色时,您只需将此样式与小部件相关联:

Next, you simply associate this style with the widget when you want the color to change:

my_checkbutton.configure(style="Red.TCheckbutton")

学习如何使用 ttk 样式的最佳资源是 tkdocs.com.具体来说,https://www.tkdocs.com/tutorial/styles.html.

The best resource for learning how to work with the ttk styles is tkdocs.com. Specifically, https://www.tkdocs.com/tutorial/styles.html.

这是一个完整的工作示例:

Here's a complete working example:

import ttk
import Tkinter as tk

class ExampleApp(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.var1 = tk.StringVar()
        self.var2 = tk.StringVar()

        f1 = ttk.Frame(self)
        red_button = ttk.Button(f1, text="Red", command=self.make_red)
        default_button = ttk.Button(f1, text="Default", command=self.make_default)

        red_button.pack(side="left")
        default_button.pack(side="left")

        f2 = ttk.Frame(self)
        self.cb_one = ttk.Checkbutton(f2, text="Option 1", variable=self.var1,
                                      onvalue=1, offvalue=0)
        self.cb_two  = ttk.Checkbutton(f2, text="Option 2", variable=self.var2,
                                       onvalue=1, offvalue=0)
        self.cb_one.pack(side="left")
        self.cb_two.pack(side="left")

        f1.pack(side="top", fill="x")
        f2.pack(side="top", fill="x")

        style = ttk.Style()
        style.configure("Red.TCheckbutton", foreground="red")

    def make_red(self):
        self.cb_one.configure(style="Red.TCheckbutton")
        self.cb_two.configure(style="Red.TCheckbutton")

    def make_default(self):
        self.cb_one.configure(style="TCheckbutton")
        self.cb_two.configure(style="TCheckbutton")


if __name__ == "__main__":
    root = tk.Tk()
    ExampleApp(root).pack(fill="both", expand=True)
    root.mainloop()

这篇关于更改 ttk 小部件文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10