tew
tew

Reputation: 2831

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?

Upvotes: 3

Views: 837

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386210

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

Upvotes: 3

Related Questions