- 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 do I get the background color of a Tkinter Canvas widget?
Tkinter canvas widget is used for many different purposes such as adding objects, drawing shapes, images and complex visuals to a graphical interface in an application. We can also configure its style such as background color, foreground color, and other properties using the configure properties or passing attributes.
Suppose we want to inherit the background color of the Canvas widget in another widget or in some part of the application. This can be achieved by using my_canvas["background"] property. Further, we can use canvas["background"] to fetch the background color of the canvas widget.
Example
# Import the required library from tkinter import * from tkinter import ttk # Create an instance of tkinter frame win = Tk() win.geometry("700x350") # Add a Canvas widget canvas = Canvas(win, background= "white") # Draw a rectangle in Canvas and inherit the background color of Canvas canvas.create_rectangle(50,50,350,190, outline="black", fill= canvas["background"]) canvas.pack() win.mainloop()
Output
Executing the above code will display a window with a rectangle having the same background color as the canvas.
- Related Articles
- How to change the background color of a tkinter Canvas dynamically?
- Dynamically change the widget background color in Tkinter
- How to change the menu background color of Tkinter's OptionMenu widget?
- How do I get an event callback when a Tkinter Entry widget is modified?
- How do I change the background of a Frame in Tkinter?
- How do I center the text in a Tkinter Text widget?
- How do I update images on a Tkinter Canvas?
- How to make a Button using the Tkinter Canvas widget?
- How do I give focus to a python Tkinter text widget?
- How do I find out the size of a canvas item in Tkinter?
- Python Tkinter – How do I change the text size in a label widget?
- How do I change the background color of the ActionBar of an ActionBarActivity?
- How to set the background color of a ttk.Combobox in tkinter?
- How to reset the background color of a Python Tkinter button?
- How to change the background color of a Treeview in Tkinter?

Advertisements