简体   繁体   English

在将文本输入文本小部件后按Enter键时如何执行回调?

[英]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.

相关问题 按下回车键后显示文本 - show text after enter key pressed 在Tkinter中按下Tab键后,如何捕获文本小部件的值? - How to Capture the Value of a Text Widget after the Tab Key is Pressed in Tkinter? 如何在按下Enter后的If / Else语句中使用Text小部件中的最后一个用户输入? - How to use the last user input in Text widget in an If/Else statement after Enter is pressed? 在python中按下键盘上的“ enter”键时,如何设置特定的动作 - How do I set a specific action to happen when the “enter” key on my keyboard is pressed in python 如何正确地将我在计算器中按下的数字插入到最后一个索引处的文本小部件中? - How do I properly insert the numbers that I pressed on in my calculator into the text widget at the last index? 在PysimpleGUI的输入法中输入文字后,如何自动将光标置于输入文字的末尾? - After entering a text in the input of PysimpleGUI, how do I put the cursor to the end of the input text automatically? 我如何知道 QTextEdit 上的 Enter 键何时被按下 - How can I know when the Enter key was pressed on QTextEdit 按下certian键时,如何让python执行函数? - How do I make python execute a function when a certian key is pressed? 我如何让用户逐行输入单词,直到按下Enter键? - How do I have a user enter words line by line until the Enter key is pressed? 在wxPython的文本框中按Enter键时获取文本 - Get text when enter is pressed in a text box in wxPython
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM