- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
- Related Articles
- Dynamically change the widget background color in Tkinter
- How to change the background color of a Treeview in Tkinter?
- How to change background color of a canvas circle using Fabric.js ?
- How do I get the background color of a Tkinter Canvas widget?
- How to change selection background color of a canvas circle using Fabric.js?
- How to change background color of canvas-type text using Fabric.js?
- How to make a Button Hover to change the Background Color in Tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- How to change the width of a Frame dynamically in Tkinter?
- How to reset the background color of a Python Tkinter button?
- How to set the background color of a ttk.Combobox in tkinter?
- How to change the color of a Tkinter label programmatically?
- How to fully change the color of a Tkinter Listbox?
- How to change the background color using jQuery?
- How to create a canvas with a background color using FabricJS?
