Ragaxus
Ragaxus

Reputation: 35

Returning object information after clicking its representation in a Tkinter canvas

I have a bunch of objects interacting with each other. They all draw ovals that constitute representations of themselves on a Tkinter Canvas, and store the representation's Tkinter id as an attribute.

How can I make it so when I click one of the ovals, my program will print the other attributes of the object which that oval represents?

Upvotes: 0

Views: 218

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386210

When you click on an item, you can get the canvas item id of the one that was clicked on. You would then use this information to look up the actual object. You can either iterate over all your objects looking for one with the given id, or you can keep a dictionary which maps ids to objects. Once you have the actual object, you can print the other attributes however you want.

Upvotes: 1

Related Questions