Python 如何在tkinter中为textbox编写事件?

Python 如何在tkinter中为textbox编写事件?,python,events,tkinter,Python,Events,Tkinter,我用tkinter的python 3.6.2编写了一个程序,我编写了以下代码: from tkinter import * root=Tk() def retrieve_input(): inputValue=textBox.get("1.0","end-1c") print(inputValue) textBox=Text(root, height=2, width=10) textBox.pack() buttonCommit=Button(root, height=1,

我用tkinter的python 3.6.2编写了一个程序,我编写了以下代码:

from tkinter import *
root=Tk()
def retrieve_input():
    inputValue=textBox.get("1.0","end-1c")
    print(inputValue)

textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit", 
                    command=lambda: retrieve_input())
buttonCommit.pack()
mainloop()
我想在文本框中按Enter键时打印“您在文本框中按Enter键”。 如何写入事件?

检查此项

from tkinter import *
from tkinter import messagebox

root=Tk()

def alertme(*args):
    messagebox.showinfo("Message", "you press Enter in textbox")

def retrieve_input():
    inputValue=textBox.get("1.0","end-1c")
    print(inputValue)

textBox=Text(root, height=2, width=10)
textBox.pack()

textBox.bind("<Return>", alertme) #this line calls alertme function whenever you press Enter key

buttonCommit=Button(root, height=1, width=10, text="Commit", 
                    command=lambda: retrieve_input())
buttonCommit.pack()
mainloop()
从tkinter导入*
从tkinter导入消息框
root=Tk()
def alertme(*args):
messagebox.showinfo(“消息”,“在文本框中按Enter键”)
def检索_输入():
inputValue=textBox.get(“1.0”,“end-1c”)
打印(输入值)
文本框=文本(根,高度=2,宽度=10)
textBox.pack()
textBox.bind(“,alertme)#只要按Enter键,这一行就会调用alertme函数
buttonCommit=按钮(根,高度=1,宽度=10,text=“提交”,
command=lambda:retrieve_input())
buttonCommit.pack()
mainloop()

现在它只能在文本框中工作…

您做过任何研究吗?绑定到事件是非常基本的,几乎在每个tkinter教程中都有介绍。无论光标在哪个小部件中,这都会打印出来。由于这个程序只有一个输入小部件,这一点是没有意义的,但是对于一个有多个输入小部件的程序来说,这不是一个好的解决方案。你不应该绑定到
root
@Bryan Oakley如何修复它?绑定到文本小部件