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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With