How to 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()
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook