Search code examples
pythontkinterclicktk-toolkitrubiks-cube

Tkinter - How To Get Tag Name from Clicking on Rectangle


I'm fairly new to programming, so appoloigise for any inconsistencies / using code incorrectly)

I've seen a few similar questions and answers on this topic, however I feel like I may be missing something.

I've drawn a net of a Rubiks Cube, and I want the user to be able to click on an individual 'cubie' to change its colour, so it will filter through the 6 colours. Basically, what I'm trying to figure out is how to access the tag of the rectangle, from the tag_bind method.

Here is a simplified version of the code I have used:

def clicked(event):
    print("You clicked " + str(event))
    print(event.widget.find_withtag("current"))


green_00 = cubeCanvas.create_rectangle(20, 240, 90, 310, width=0, fill='green', tag="green_00")
cubeCanvas.tag_bind("green_00", "<Button-1>", clicked)

This currently returns:

You clicked <ButtonPress event num=1 x=56 y=299>
(1,)

Whereas ideally I want it to return:

green_00

The aim would then be to use the tag, to identify the rectangle in itemconfig - so that I can change the colour. Any answer / a better way to approach this problem is greatly appreciated.

Thanks.


Solution

  • Thanks to Atlas435 - I was missing something very small.

    current = event.widget.find_withtag("current")[0]
    event.widget.itemconfig(current, *option)
    

    Allows you to access and then change the colour of the specific tag.