Search code examples
pythontkintertuplescoordstkinter-canvas

Tkinter canvas.coords method not working with tuple


I'm having a bit of trouble with the following code - No matter what I try, the coords function returns an empty list when I use a tuple of tags.

def drop_line(self, event):
    """
    Function to call when the mouse is released. Creates a permanent bond between people.
    """
    self.delete_temporary()
    person = self.canvas.find_enclosed(event.x - self.people_size_var.get(),
                                       event.y - self.people_size_var.get(),
                                       event.x + self.people_size_var.get(),
                                       event.y + self.people_size_var.get())

    # Writing friendships to dictionary.
    try:
        Link.person_name = self.canvas.gettags(person)
        Link.friends_dictionary[Link.person_name[0]] = (Link.friend_name[0] + "," +
                                                        Link.friends_dictionary[Link.person_name[0]])
        Link.friends_dictionary[Link.friend_name[0]] = (Link.person_name[0] + "," +
                                                        Link.friends_dictionary[Link.friend_name[0]])
        line = self.canvas.create_line(self.pos_x, self.pos_y, event.x, event.y, activefill="red", smooth=True,
                                       fill="black",width=self.edge_width_var.get(),
                                       tags=("permanent", Link.person_name[0], Link.friend_name[0]))


        # My problem is here :( Can't get the last 2 prints to work

        print(self.canvas.gettags(line))
        print(("permanent", Link.person_name[0], Link.friend_name[0]))
        print(self.canvas.coords(("permanent", Link.person_name[0], Link.friend_name[0])))
        print(self.canvas.coords(self.canvas.gettags(line)))

Any help is appreciated, even ideas or links!

Thank you.


Solution

  • What makes you think you can pass a tuple of tags to canvas.coords?
    As you can read on effbot.org, The New Mexico Tech website and in the Tk Manual you can pass an id or a tag. It will then return the coordinates of the first matching item or you can pass new coordinates to it.

    Tags are meant mainly to group objects together under the same tag. The fact that your line has multiple tags just means it can be found using any of those tags. I don't know of any method that searches for tag combinations. If you want to find one specific object, give it a unique tag or use it's id.