3

My issue is a kind of follow up of this question

I would like to get the widget which has been right clicked to do an action on it.

Conditions :

  1. right click on "my label 2"
  2. select "Display the label"

Expected results :

=> "my label 2" should be printed

Initial code to solve :

from Tkinter import * # Tkinter -> tkinter in Python 3

root = Tk()

def print_label():
    print "Please, help me to print the label which has been clicked !"

def popup(event):
    menu.post(event.x_root, event.y_root)


# create a popup menu
menu = Menu(root, tearoff=0)
menu.add_command(label="Display the label", command=print_label)


# create the 3 labels
label1_text=StringVar()
label2_text=StringVar()
label3_text=StringVar()

label1_text.set("my label 1")
label2_text.set("my label 2")
label3_text.set("my label 3")

label1=Label(root, textvariable=label1_text)
label2=Label(root, textvariable=label2_text)
label3=Label(root, textvariable=label3_text)

label1.pack()
label2.pack()
label3.pack()

# attach popup to frame
label1.bind("<Button-3>", popup)
label2.bind("<Button-3>", popup)
label3.bind("<Button-3>", popup)

root.mainloop()

1 Answer 1

3

Making as few changes as possible to the current code, you need to do three things:

  1. Create a global variable to hold a reference to the currently selected label.

  2. Inside popup, set this variable to event.widget, which will be the currently selected label.

  3. Inside print_label, print this label's text by accessing its "text" key.

Below is a fixed version of your program. The stuff I changed is in the comment box:

from Tkinter import * # Tkinter -> tkinter in Python 3

root = Tk()

#############################################################
selected = None  # This is the variable mentioned in step 1

def print_label():
    print selected["text"]  # This is step 3

def popup(event):
    global selected  # Tell Python that selected is global

    menu.post(event.x_root, event.y_root)

    selected = event.widget  # This is step 2
#############################################################

# create a popup menu
menu = Menu(root, tearoff=0)
menu.add_command(label="Display the label", command=print_label)


# create the 3 labels
label1_text=StringVar()
label2_text=StringVar()
label3_text=StringVar()

label1_text.set("my label 1")
label2_text.set("my label 2")
label3_text.set("my label 3")

label1=Label(root, textvariable=label1_text)
label2=Label(root, textvariable=label2_text)
label3=Label(root, textvariable=label3_text)

label1.pack()
label2.pack()
label3.pack()

# attach popup to frame
label1.bind("<Button-3>", popup)
label2.bind("<Button-3>", popup)
label3.bind("<Button-3>", popup)

root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I did not know event.widget :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.