简体   繁体   中英

How to change the foreground color of ttk.Button when its state is active?

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')

And it works fine (screenshot):

在此输入图像描述

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

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

EDIT1 : After this:

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')   

I get the error:

backButton.configure(activeforeground='gray') NameError: global name 'backButton' is not defined .

First approach: 2 functions bound to 2 different events

You need to play around with tkinter events.

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.

Cheaper approach: 1 function bound to 1 event

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')

Then bind this function to the Enter event as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM