Reputation: 21
So I have two shapes in tkinter python3.0, with one shape enclosed inside the other. I have given them both different tags. On click, I want to fill the shape selected, with the color being different depending on the item's tag.
Upon trying this, I found that if I filled the inner shape first, I could fill the outer shape fine. However, if I did the opposite and filled the outer shape first, I could not fill in the inner shape for some reason. I tried doing tag_lower() and tag_raise(), but they don't change anything.
Below is the code:
from tkinter import *
root = Tk()
canvas = Canvas(root,width=200,height=200,bg="white")
canvas.grid()
firstRect = canvas.create_rectangle(10,10,30,30, tag="in")
secondRect = canvas.create_rectangle(15,15,25,25, tag="out")
def onclick(event):
item = canvas.find_closest(event.x, event.y)
tags = canvas.gettags(item)
if tags[0] == "in":
canvas.itemconfig(item, fill="red")
else:
canvas.itemconfig(item, fill="blue")
canvas.bind('<Button-1>', onclick)
I cannot fill the inner shape after the outer one has been filled. How would I go about this issue? Thanks.
Upvotes: 1
Views: 332
Reputation: 21
Add a inital fill: .create_rectangle(..., tag="out", fill='white') – stovfl Thanks to stovfl for finding out the problem
Upvotes: 1