我有一个带有条目的 UI,以及一个使用该条目部署多个功能的按钮。
ent = tk.Entry(frm_top, textvariable=entry_input)
btn_confirm = tk.Button(frm_inner_bot, text="Confirm", width=25, height=2, command=lambda:
[main.save_template(entry_input.get(),filedialog.askdirectory()),update_main_window(label, listbox), create_template_window.destroy()])
我希望能够从条目中按下并执行与按下按钮相同的功能,但是我无法做到这一点。
我尝试使用绑定命令并简单地用 lambda 填充它,但它不起作用。
ent.bind('<Return>', lambda:[main.save_template(entry_input.get(),filedialog.askdirectory()),update_main_window(label, listbox), create_template_window.destroy()])
我也尝试过按下按钮,但它只是一次执行所有操作,而不是像按下按钮那样一次执行 1 个操作。
ent.bind('<Return>', btn_confirm.invoke())
如果有人能向我解释我哪里出错了,以及如何解决我的问题,我将不胜感激。
阅读了有关此事的多篇文章和教程,但找不到可以通过绑定解决多个功能部署的文章和教程。
您应该将条目小部件的父窗口与“Return”绑定,并像将按钮与功能映射一样。比如说..
from tkinter import *
def function to be triggered():
user_entry = ent.get()
# Do something
root = Tk()
root.geometry('300x200')
ent = Entry(root)
ent.pack()
root.bind('<Return>', function_to_be_triggered)
button(root, command=function_to_be_triggered)
button.pack()
root.mainloop()