[英]How do I execute a callback when the Enter key is pressed after entering text into a Text widget?
I've really tried to find this out but have only discovered disconnected snippets such as tag_bind
. 我真的试图找到它,但只发现了断开连接的代码段,如
tag_bind
。 I cannot work out how to use this (if indeed this is the right way forward). 我无法弄清楚如何使用它(如果确实这是正确的方法)。
I've got a Text widget into which the user can enter some text (such as a command or question) and I'd like to execute a function that reads that text then responds to it. 我有一个Text小部件,用户可以在其中输入一些文本(例如命令或问题),我想执行一个函数来读取该文本然后响应它。 Obviously I could provide a nearby Button widget which when clicked, reads the text in the Text widget.
显然,我可以提供一个附近的Button小部件,当单击它时,它会读取Text小部件中的文本。 But I don't want people to have to move their hand from the keyboard to the mouse.
但我不希望人们不得不将手从键盘移到鼠标上。 I'd just like the callback to be triggered if they hit the Enter key.
我只是想在回到Enter键时触发回调。 How do I do it please?
我该怎么办?
import Tkinter as tk
def on_enter(event):
widget = event.widget
print(widget.get())
root = tk.Tk()
entry = tk.Entry()
entry.pack(padx=5, pady=5)
entry.focus()
entry.bind('<Return>', on_enter)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.