简体   繁体   English

如何重置python tkinter按钮的背景颜色?

[英]How to reset background color of a python tkinter button?

Simple question, have parsed numerous search results, haven't found a simple answer.简单的问题,解析了无数搜索结果,还没有找到简单的答案。 I set a very urgent button to red if something is not found so users click it first.如果找不到某些东西,我将一个非常紧急的按钮设置为红色,以便用户先单击它。 Once clicked, I want to set the background back to normal like button.config(bg='') .单击后,我想将背景设置为正常,例如button.config(bg='') I tried 'grey' but it's not the right color.我试过“灰色”,但颜色不对。

What do I set it to?我把它设置成什么?

You can ask the button what color it is before you change it.您可以在更改按钮之前询问它是什么颜色。 Save the color, and then restore it later:保存颜色,稍后再恢复:

orig_color = the_button.cget("background")
the_button.configure(background="red")
...
the_button.configure(background=orig_color)

The default colour for buttons is SystemButtonFace .按钮的默认颜色是SystemButtonFace I am not sure how many versions of Windows this spans back to, but it is the default system colour for buttons.我不确定这跨越了多少个 Windows 版本,但它是按钮的默认系统颜色。

You can find the default colours by using @Bryan Oakley's answer above, and then print() it to the console.您可以使用上面@Bryan Oakley 的答案找到默认颜色,然后将其print()到控制台。

I use the code:我使用代码:

def ToGray(self, to_gray):
    to_gray['bg'], to_gray['fg'] = "SystemButtonFace", "Black"

Then you can get the default button or label.然后您可以获得默认按钮或标签。

The default background color of tkinter is '#f0f0f0'. tkinter 的默认背景颜色是“#f0f0f0”。 Hope this helps ;)希望这会有所帮助 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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