简体   繁体   English

如何在 tkinter 中更改默认背景颜色

[英]How to change default background colour in tkinter

I want to have all my labels and buttons to have a white background.我想让我所有的标签和按钮都有一个白色的背景。 I know I can use bg="white" in each one but I was thinking if there was a way I could change the default colour to white or make it so that all widgets have a white background with a single line of code.我知道我可以在每一个中使用bg="white"但我在想是否有一种方法可以将默认颜色更改为白色或使其具有单行代码的白色背景。 similar to how you can change the font by doing:类似于如何通过以下方式更改字体:

from tkinter import *
window=Tk()
window.option_add("*font",("Arial",15))
window.mainloop()

Which will set all the font to Arial 15这会将所有字体设置为 Arial 15

thanks to @acw1668 's comment, can do window.option_add("*Label*Background", "white") and window.option_add("*Button*Background", "white") to solve it.感谢@acw1668 的评论,可以做window.option_add("*Label*Background", "white")window.option_add("*Button*Background", "white")来解决它。 Just thought id put it as an answer in case people don't see the comment and what not.只是想我把它作为一个答案,以防人们看不到评论和什么。

Unfortunately, as far as I'm aware there is no such natively supported function.不幸的是,据我所知,没有这种本机支持的功能。 However, you could make your own custom method to do just this.但是,您可以创建自己的自定义方法来执行此操作。 Here's an idea of what I'm talking about:这是我在说什么的一个想法:

def whiteButton(window, text, fg):
  return Button(window, text = text, bg='white', fg=fg)

I hope that was helpful:)我希望这有帮助:)

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

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