- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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()
It will produce the following output −
Clicking the "Change Color" button would change the background color of the canvas.
- Dynamically change the widget background color in Tkinter
- How to change the background color of a Treeview in Tkinter?
- How do I get the background color of a Tkinter Canvas widget?
- 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?
- Change the background color of a button with CSS
- How to change the background color of the font in PowerShell?
- How to change the color of a Tkinter rectangle on clicking?
- How to create a canvas with a background color using FabricJS?