Default window colour Tkinter and hex colour codes

时光怂恿深爱的人放手 提交于 2019-12-18 20:10:24

问题


I would like to know the default window colour in Tkinter when you simply create a window:

root = Tk()

If there is one, it is possible to set widgets to the same colour or use a hex colour code? (using rgb)

The colour code I have found for the 'normal' window is:

R = 240, G = 240, B = 237

Thanks.


回答1:


Not sure exactly what you're looking for, but will this work?

import Tkinter

mycolor = '#%02x%02x%02x' % (64, 204, 208)  # set your favourite rgb color
mycolor2 = '#40E0D0'  # or use hex if you prefer 
root = Tkinter.Tk()
root.configure(bg=mycolor)
Tkinter.Button(root, text="Press me!", bg=mycolor, fg='black',
               activebackground='black', activeforeground=mycolor2).pack()
root.mainloop()

If you just want to find the current value of the window, and set widgets to use it, cget might be what you want:

import Tkinter

root = Tkinter.Tk()
defaultbg = root.cget('bg')
Tkinter.Button(root,text="Press me!", bg=defaultbg).pack()
root.mainloop()

If you want to set the default background color for new widgets, you can use the tk_setPalette(self, *args, **kw) method:

root.tk_setPalette(background='#40E0D0', foreground='black',
               activeBackground='black', activeForeground=mycolor2)
Tkinter.Button(root, text="Press me!").pack()

Then your widgets would have this background color by default, without having to set it in the widget parameters. There's a lot of useful information provided with the inline help functions import Tkinter; help(Tkinter.Tk)




回答2:


rudivonstaden's answer led me to a solution to the problem, although for some reason root.cget("bg") fails because "bg" is an unknown color name.

However, knowing that a widget has a dictionary containing its properties means that root["bg"] returns the background color of the widget.

So if you create a window named myWindow without overriding your system's default background color, then myWindow["bg"] is the default background color for a window, which can be used when creating frameless text fields within that window.




回答3:


I was trying to set a button's color to the system default. This is the best solution I've come across:

root.configure(background='SystemButtonFace')

Source: https://stackoverflow.com/a/53460702/10346152




回答4:


The default color for Tkinter window I found was #F0F0F0




回答5:


some_widget(bg=some_widget._root().cget('bg'))



来源:https://stackoverflow.com/questions/11340765/default-window-colour-tkinter-and-hex-colour-codes

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