Set Tkinter Background Color
Tkinter widget class method configure
and attribute bg
or background
could be used to set the Tkinter widget/window background color.
Method configure(background= )
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("configure method")
app.geometry('300x200')
app.configure(bg='red')
app.mainloop()
Here,
app.configure(bg='red')
configures the background color of app
to be red
. You could also use background
instead of bg
.
app.configure(background='red')
Attribute bg
or background
background
or bg
is one attribute in most of Tkinter widgets, and could be used to set the background color directly.
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("bg attribute")
app.geometry('300x200')
app['bg'] = 'blue'
app.mainloop()
Tkinter Color Code
You could specify the color with the known names like red
, blue
or green
as shown above, and you could also specify it via RGB like hex color codes in HTML, e.g, #49A
or #0059b3
.
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("bg attribute")
app.geometry('300x200')
app['bg'] = '#0059b3'
app.mainloop()
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("bg attribute")
app.geometry('300x200')
app['bg'] = '#49A'
app.mainloop()
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.