我想使选择下的文本变量'a'显示为菜单的标签。 这里是代码:将文本传递到tkinter中的菜单标签
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)
现在,我所得到的是功能弹出地址。 如果我尝试popup.a,我得到一个错误,函数没有属性'a'。我如何克服这一点,并得到'a'中的任何内容作为菜单标签打印?
AFAIK,回调方法不应该返回任何东西。尝试在'popup'方法内的菜单中添加'a',即在那里移动'menu.add_command(label = a,command = ...)'。 –
@tobias_k - 它的工作,非常感谢。我已经尝试过,但显然有一个错字或什么的。 – kaboom
很高兴我能帮到你。我已经添加了这个答案。 –