How to change the background color of a tkinter Canvas dynamically?


The Canvas widget is one of the most useful widgets in Tkinter. It has various functionalities and features to help developers customize the application according to their need. The Canvas widget is used to display the graphics in an application. You can create different types of shapes and draw objects using the Canvas widget.

To change the background color of the Canvas widget, you can use the configure() method. Here, you can specify the background color of the Canvas widget which you want to change explicitly.


In the following example, we have created a canvas widget with a default background color "skyblue", which can be changed after its creation.

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
win= Tk()

# Define the size of the window

# Function to change the color of the canvas
def change_color():

# Create a canvas widget
canvas= Canvas(win, bg='skyblue')

# Create a button
button=Button(win, text= "Change Color", font=('Helvetica 10 bold'), command=change_color)



It will produce the following output −

Clicking the "Change Color" button would change the background color of the canvas.

Published on 22-Dec-2021 08:11:43