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

匆匆过客 提交于 2020-07-09 12:39:05

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/44323528/how-to-change-the-foreground-color-of-ttk-button-when-its-state-is-active

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!