64

I'm writing a slideshow program with Tkinter, but I don't know how to change the background color to black instead of the standard light gray. How can this be done?

import os, sys
import Tkinter
import Image, ImageTk
import time

root = Tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: e.widget.quit())
image = Image.open(image_path+f)
tkpi = ImageTk.PhotoImage(image)        
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=w,height=h)
root.mainloop(0)
olofom
  • 6,233
  • 11
  • 37
  • 50
  • 1
    background of what? a widget? use `background` keyword. More infos: http://www.pythonware.com/library/tkinter/introduction/widget-styling.htm – mg. Apr 30 '10 at 13:52
  • Many Tk widgets have bg attribute, which allows to specify their background colour. – sastanin Apr 30 '10 at 13:57
  • Well, I'm really new to Tk so I'm not sure about exactly what a widget is, but label_image.configure(background='black') made the trick. Is label_image in this case a widget or is it only root? – olofom Apr 30 '10 at 18:26
  • Classic Tk widgets have the attribute. Themed ones tend not to; the background color of a widget is a property of the style/theme there. – Donal Fellows May 02 '10 at 10:53

5 Answers5

131
root.configure(background='black')

or more generally

<widget>.configure(background='black')
msw
  • 42,753
  • 9
  • 87
  • 112
  • Thanks a lot! In this case label_image.configure(background='black') was what needed to get a black background! – olofom Apr 30 '10 at 18:25
  • Can only be predefined colors like "black" or "white" be used or also hex codes? – Noah Krasser Oct 04 '17 at 16:43
  • As far as I know, [any proper color](http://effbot.org/tkinterbook/tkinter-widget-styling.htm) is okay. Here's [a simple example](https://gist.github.com/thecjharries/8a4ecf94d2b43564d9b87815a3d1de55). – CJ Harries Feb 11 '18 at 11:30
  • 2
    For posterity, colors that work are those defined by the system (e.g. [lots of named ones](http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter)) or hex values, which can be of the forms `#RGB`, `#RRGGBB`, and `#RRRRGGGGBBBB`. – CJ Harries Feb 11 '18 at 11:39
24

I know this is kinda an old question but:

root["bg"] = "black"

will also do what you want and it involves less typing.

  • Interesting - I was unaware that we could use [] too. All the examples I found were either using .configure() or, in ruby, the {} block variant. I do, however had, oddly prefer the slightly longer name "background". bg is shorter but I think a tiny bit verbosity is ok. – shevy May 20 '21 at 11:54
4

config is another option:

widget1.config(bg='black')
widget2.config(bg='#000000')

or:

widget1.config(background='black')
widget2.config(background='#000000')
Nae
  • 14,209
  • 7
  • 52
  • 79
4

Its been updated so

root.configure(background="red")

is now:

root.configure(bg="red")
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
3
widget['bg'] = '#000000'

or

widget['background'] = '#000000'

would also work as hex-valued colors are also accepted.

Nae
  • 14,209
  • 7
  • 52
  • 79