Changing ttk widget text color(更改 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 小部件文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改 ttk 小部件文本颜色
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
