Reputation:
I want to make the text under the selection, in variable 'a' to appear as a label of the menu. Here is the code:
def popup(event):
a=t_start.get("sel.first", "sel.last")
menu.post(event.x_root, event.y_root)
return a
def insert_word():
pass
t_start.bind("<Button-3>", popup)
menu = Menu(root, tearoff=0)
menu.add_command(label="Set selection 1", command=set_selection_1)
menu.add_command(label="Set selection 2", command=set_selection_2)
menu.add_command(label="%s" %popup, command=set_selection_2)
Right now, all I get is function popup address. If I try popup.a, I get an error, function has no attribute 'a'. How do I overcome this and get whatever is in 'a' to be printed as menu label?
Upvotes: 0
Views: 566
Reputation: 82899
Callback methods like popup
are not supposed to return anything. Instead, you should manipulate the menu
from inside the function.
Also, as suggested by Brian, you probably rather want to modify an existing entry in the menu, instead of adding a new one each time you click the button. In this case, create the entry outside the function (like you do now), but use some placeholder for the label.
def popup(event):
a = t_start.get("sel.first", "sel.last")
menu.post(event.x_root, event.y_root)
menu.add_command(label=a, command=set_selection_2) # add to menu
# menu.entryconfig(2, label=a) # modify existing entry
Upvotes: 1
Reputation: 385970
If you want to change the text on a menu, you must use the entryconfig
method. For example, to change the text of the first item you would do:
menu.entryconfig(0, label=a)
Upvotes: 1