
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change 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.
Example
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 win.geometry("700x300") # Function to change the color of the canvas def change_color(): canvas.configure(bg='blue') # Create a canvas widget canvas= Canvas(win, bg='skyblue') canvas.pack() # Create a button button=Button(win, text= "Change Color", font=('Helvetica 10 bold'), command=change_color) button.pack() win.mainloop()
Output
It will produce the following output −
Clicking the "Change Color" button would change the background color of the canvas.