How do I get the background color of a Tkinter Canvas widget?

Tkinter Canvas widget is used for drawing shapes, images and complex visuals in GUI applications. You can configure its properties like background color using the configure() method or by passing attributes during creation.

To get the background color of a Canvas widget, you can use the dictionary-style access canvas["background"] or the cget() method. This is useful when you want to inherit the canvas background color in other widgets or parts of your application.

Using Dictionary-Style Access

The most common way to get the background color ?

import tkinter as tk

# Create main window
root = tk.Tk()
root.geometry("400x300")

# Create canvas with white background
canvas = tk.Canvas(root, background="lightblue", width=300, height=200)
canvas.pack(pady=20)

# Get background color using dictionary access
bg_color = canvas["background"]
print(f"Canvas background color: {bg_color}")

# Create a rectangle with the same background color as canvas
canvas.create_rectangle(50, 50, 250, 150, 
                       outline="black", 
                       fill=canvas["background"])

root.mainloop()
Canvas background color: lightblue

Using cget() Method

Alternative approach using the cget() method ?

import tkinter as tk

# Create main window  
root = tk.Tk()
root.geometry("400x300")

# Create canvas with specific background
canvas = tk.Canvas(root, bg="yellow", width=300, height=200)
canvas.pack(pady=20)

# Get background color using cget method
bg_color = canvas.cget("background")
print(f"Background color using cget(): {bg_color}")

# You can also use the short form 'bg'
bg_color_short = canvas.cget("bg")
print(f"Background color using 'bg': {bg_color_short}")

root.mainloop()
Background color using cget(): yellow
Background color using 'bg': yellow

Practical Example

Creating multiple shapes that inherit the canvas background color ?

import tkinter as tk

root = tk.Tk()
root.geometry("500x400")
root.title("Canvas Background Color Demo")

# Create canvas with light green background
canvas = tk.Canvas(root, background="lightgreen", width=400, height=300)
canvas.pack(pady=20)

# Get the background color
canvas_bg = canvas["background"]

# Create shapes using the canvas background color
canvas.create_rectangle(50, 50, 150, 100, 
                       outline="black", 
                       fill=canvas_bg, 
                       width=2)

canvas.create_oval(200, 50, 300, 150, 
                  outline="red", 
                  fill=canvas_bg, 
                  width=2)

# Add text showing the color
canvas.create_text(200, 200, 
                  text=f"Canvas BG: {canvas_bg}", 
                  font=("Arial", 12))

root.mainloop()

Comparison

Method Syntax Usage
Dictionary Access canvas["background"] Most common, concise
cget() Method canvas.cget("background") More explicit, readable
Short Form canvas.cget("bg") Works with abbreviated attribute

Conclusion

Use canvas["background"] for quick access to the canvas background color. The cget() method provides a more explicit approach and works with both full and abbreviated attribute names.

Updated on: 2026-03-25T22:29:45+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements