<small id='vKNtP'></small><noframes id='vKNtP'>

      <bdo id='vKNtP'></bdo><ul id='vKNtP'></ul>
    1. <tfoot id='vKNtP'></tfoot>

      <i id='vKNtP'><tr id='vKNtP'><dt id='vKNtP'><q id='vKNtP'><span id='vKNtP'><b id='vKNtP'><form id='vKNtP'><ins id='vKNtP'></ins><ul id='vKNtP'></ul><sub id='vKNtP'></sub></form><legend id='vKNtP'></legend><bdo id='vKNtP'><pre id='vKNtP'><center id='vKNtP'></center></pre></bdo></b><th id='vKNtP'></th></span></q></dt></tr></i><div id='vKNtP'><tfoot id='vKNtP'></tfoot><dl id='vKNtP'><fieldset id='vKNtP'></fieldset></dl></div>

    2. <legend id='vKNtP'><style id='vKNtP'><dir id='vKNtP'><q id='vKNtP'></q></dir></style></legend>

        更改 ttk 小部件文本颜色

        时间:2023-09-03
        <legend id='giCB6'><style id='giCB6'><dir id='giCB6'><q id='giCB6'></q></dir></style></legend>

        1. <small id='giCB6'></small><noframes id='giCB6'>

            <bdo id='giCB6'></bdo><ul id='giCB6'></ul>
          • <tfoot id='giCB6'></tfoot>

              <tbody id='giCB6'></tbody>
              <i id='giCB6'><tr id='giCB6'><dt id='giCB6'><q id='giCB6'><span id='giCB6'><b id='giCB6'><form id='giCB6'><ins id='giCB6'></ins><ul id='giCB6'></ul><sub id='giCB6'></sub></form><legend id='giCB6'></legend><bdo id='giCB6'><pre id='giCB6'><center id='giCB6'></center></pre></bdo></b><th id='giCB6'></th></span></q></dt></tr></i><div id='giCB6'><tfoot id='giCB6'></tfoot><dl id='giCB6'><fieldset id='giCB6'></fieldset></dl></div>

                1. 本文介绍了更改 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 小部件文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:你可以在 python tkinter 中更改小部件的父级吗? 下一篇:使用 SelectDateWidget 的 Django 表单字段

                  相关文章

                2. <i id='c7BNP'><tr id='c7BNP'><dt id='c7BNP'><q id='c7BNP'><span id='c7BNP'><b id='c7BNP'><form id='c7BNP'><ins id='c7BNP'></ins><ul id='c7BNP'></ul><sub id='c7BNP'></sub></form><legend id='c7BNP'></legend><bdo id='c7BNP'><pre id='c7BNP'><center id='c7BNP'></center></pre></bdo></b><th id='c7BNP'></th></span></q></dt></tr></i><div id='c7BNP'><tfoot id='c7BNP'></tfoot><dl id='c7BNP'><fieldset id='c7BNP'></fieldset></dl></div>
                  <legend id='c7BNP'><style id='c7BNP'><dir id='c7BNP'><q id='c7BNP'></q></dir></style></legend>

                      <tfoot id='c7BNP'></tfoot>

                      • <bdo id='c7BNP'></bdo><ul id='c7BNP'></ul>

                      <small id='c7BNP'></small><noframes id='c7BNP'>