[英]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. 我有一个
ttk.Button
,我想改变它的颜色。
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: 编辑1 :在此之后:
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
. backButton.configure(activeforeground='gray') NameError: global name 'backButton' is not defined
。
You need to play around with tkinter events. 你需要玩tkinter事件。
Enter
and Leave
events will fully satisfy your goals if you create 2 corresponding functions as follows: 如果您创建2个相应的函数,则
Enter
和Leave
事件将完全满足您的目标:
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. 您可以使用文本的颜色而不是按钮的背景颜色,而是使用
foreground
选项。
A cheaper approach consists in using only the Enter
event and playing on the activeforground
option instead. 更便宜的方法包括仅使用
Enter
事件并在activeforground
选项上播放。
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: 然后将此函数绑定到
Enter
事件,如下所示:
backButton.bind('<Enter>', update_active_foreground_color_when_mouse_enters_button)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.