问题
Does anyone know the colour code for the default background? I cant seem to find this anywhere. In my program I changed the background colour and need to change it back to the default colour later on but I am unable to find the colour code.
Any help is appreciated. Thanks.
回答1:
Try this:
root.configure(background='SystemButtonFace')
回答2:
If you want to get the default background at runtime, you can use the cget
method. This may return a color name rather than an rgb value.
import Tkinter as tk
root = tk.Tk()
bg = root.cget("background")
# eg: 'systemWindowBody'
You can convert that to a tuple of the red, green and blue components
rgb = root.winfo_rgb(bg)
# eg: (65535, 65535, 65535)
You can then format the value as a hex string if you wish:
color = "#%x%x%x" % rgb
# eg: '#ffffffffffff'
To reset the background after changing it, save the value, and then use the value with the configure
command:
original_background = root.cget("background")
...
root.configure(background=original_background)
回答3:
Another option is just to clear the background
setting.
For example
import Tkinter as tk
root = tk.Tk()
lbl_status = ttk.Label(root, width=20, text="Some Text")
lbl_status['background'] = 'yellow' # Set background to yellow
lbl_status['background'] = '' # Reset it to system default
来源:https://stackoverflow.com/questions/35381385/how-to-set-default-background-colour-tkinter