Daniel Casasampera
Daniel Casasampera

Reputation: 381

How to get the canvas items that have some tags?

I would like to know if theres a way to get all the items in the canvas that contain the tags. As far as I know the function canvas.find_withtag(tag) only allows for one tag and I would like to be able to do: canvas.find_withtag(tag0, tag1, ...). Basically is there a clean way to get the items that match more than one tag without having to play with sets. Is there a clean way or will I have to do the logic myself using canvas.find_withtag(tag)?

What Im actually asking is if theres is a better way to accomplish this:

itemstag1 = set(self.v_maps.canvas.find_withtag(tag1))
itemstag2 = set(self.v_maps.canvas.find_withtag(tag2))
for item in itemstag1 &itemstag2:
    self.canvas.itemconfig(item, fill=color)

Upvotes: 0

Views: 1219

Answers (1)

acw1668
acw1668

Reputation: 46751

You can use canvas.find_withtag('tag1||tag2').

From the tk document:

tagOrId may contain a logical expressions of tags by using operators: “&&”, “||”, “^”, “!”, and parenthesized subexpressions.

Upvotes: 6

Related Questions