ttk.Button处于活动状态时如何更改前景色? [英] How to change the foreground color of ttk.Button when its state is active?

查看:130
本文介绍了ttk.Button处于活动状态时如何更改前景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ttk.Button 我想要更改的颜色。

I have a ttk.Button which color I want to change.

我是通过以下方式完成的:

I did it in the following way:

Style().configure('gray.TButton', foreground='black', background='gray')              
backButton = Button(self.bottomFrame, text="Back",
                       command=lambda: controller.ShowFrame("StartPage"),  
                       style='gray.TButton')      
backButton.pack(side='left')

它很好用(截图):

但是,如果此小部件处于活动模式(其中的鼠标光标位于其中),则看起来很糟糕。背景变为白色,因此文本不可见。

But if this widget in the active mode (mouse cursor within it) it looks bad. Background becomes white so text becomes invisible.

问题:如何在 active 模式下更改文本颜色?

Question: How to change text-color in active mode?

EDIT1 :之后:

class BackSubmit(MainFrame):                             

def __init__(self, parent, controller, title):       
  MainFrame.__init__(self, parent, controller, title)

  Style().configure('gray.TButton', foreground='white', background='gray')                  
  backButton = Button(self.bottomFrame, text="Back",
                      command=lambda: controller.ShowFrame("StartPage"),
                      style='gray.TButton')          
  backButton.bind( '<Enter>', self.UpdateFgInAcSt )                                         
  backButton.pack(side='left')                       

  Style().configure('blue.TButton', foreground='blue', background='light blue')
  submitButton = Button(self.bottomFrame,            
                        text="Submit settings",   
                        command=lambda: self.submitSettings(),
                        style='blue.TButton')        
  submitButton.pack(side=RIGHT)                      

def submitSettings(self):                            
  raise NotImplementedError("Subframe must implement abstract method")

def UpdateFgInAcSt(self, event):                     
  backButton.configure(activeforeground='gray')   

我收到错误:

backButton.configure(activeforeground ='gray')
NameError:未定义全局名称'backButton'

推荐答案

第一种方法:将2个函数绑定到2个不同的事件


您需要使用 tkinter事件。

Enter Leave 事件将完全满足您的目标,如果您创建两个相应的函数,如下所示:

Enter and Leave events will fully satisfy your goals if you create 2 corresponding functions as follows:

def update_bgcolor_when_mouse_enters_button(event):
    backButton.configure(background='black') # or whatever color you want

def update_bgcolor_when_mouse_leaves_button(event):
    backButton.configure(background='gray')

然后将这两个函数绑定到您的按钮上:

Then bind these 2 functions to your button:

backButton.bind('<Enter>', update_bgcolor_when_mouse_enters_button)
backButton.bind('<Leave>', update_bgcolor_when_mouse_leaves_button)

文本的颜色而不是按钮的背景颜色,而是使用前景选项。

You can do the same with the color of the text instead of the background color of the button but using the foreground option instead.

更便宜的方法是仅使用 Enter 事件并在 activeforground 选项代替。

A cheaper approach consists in using only the Enter event and playing on the activeforground option instead.

这里您只需要定义一个函数:

Here you need to define only one function:

def update_active_foreground_color_when_mouse_enters_button(event):
   backButton.configure(activeforeground='gray')

然后将该函数绑定到 Enter 事件,如下所示:

Then bind this function to the Enter event as follows:

backButton.bind('<Enter>', update_active_foreground_color_when_mouse_enters_button)

这篇关于ttk.Button处于活动状态时如何更改前景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆