更改 ttk 小部件文本颜色

Changing ttk widget text color(更改 ttk 小部件文本颜色)
本文介绍了更改 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 小部件文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)