4

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Rezzy
  • 111
  • 1
  • 2
  • 10
  • You mean the `rgb` value ? –  Feb 13 '16 at 15:02
  • I just want to change the background colour to the default one. The RGB value or the 'word' value will help. – Rezzy Feb 13 '16 at 15:03
  • How would I know what was your default background color ? –  Feb 13 '16 at 15:03
  • Anyways there is a [rgb selector online](http://www.css3maker.com/css-3-rgba.html) to do this job. Hope this helps. –  Feb 13 '16 at 15:04
  • If you dont set a background colour when creating a button, label etc, there is a default background colour. I want to get the colour. Isn't the default background colour the same for everyone? – Rezzy Feb 13 '16 at 15:05
  • The default background colour in rgb was 240,240,240. I found that out using the paint colour picker. – Rezzy Feb 13 '16 at 15:25
  • I guess your problem is solved now. –  Feb 13 '16 at 15:26

3 Answers3

5

Try this:

root.configure(background='SystemButtonFace')
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
ErnestoQ
  • 97
  • 1
  • 5
  • 3
    Welcome to StackOverflow, your answer could be improved if you'd add a link to a documentations on that so the user could explore the subject. – Picard Nov 24 '18 at 18:04
4

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

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
cvt
  • 11
  • 1