Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have a "default color" with a canvas in Tkinter?

Is there a way I can set the default color of canvas objects (lines, rectangles, etc.) without setting each one individually? I know the default color is black, and I basically just want to change that so that everything I subsequently create is, say, green. Is there a way to do this with Tkinter in Python?

like image 931
tew Avatar asked Dec 13 '25 10:12

tew


1 Answers

No, there is no way to set the default color. However, if you store the color in a variable, you can use that whenever you create new items.

self.my_color = "red"
...
self.canvas.create_rectangle(..., fill=my_color)

You can also change all objects at once by giving the id "all" to the itemconfigure method. For example:

self.my_color = "green"
self.canvas.itemconfigure("all", fill=self.my_color)

For more on item identifiers (including the special "all" identifier) see Item Specifiers: Handles and Tags on effbot.org as well as the Tags section in the canvas tutorial on tkdocs.com

like image 85
Bryan Oakley Avatar answered Dec 16 '25 10:12

Bryan Oakley