Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter条目小部件之后的事件回调_Python_Events_Tkinter_Callback - Fatal编程技术网

Python Tkinter条目小部件之后的事件回调

Python Tkinter条目小部件之后的事件回调,python,events,tkinter,callback,Python,Events,Tkinter,Callback,从这里的第一个答案: 当用户键入以下内容时,我可以调用callback: from Tkinter import * def callback(sv): print sv.get() root = Tk() sv = StringVar() sv.trace("w", lambda name, index, mode, sv=sv: callback(sv)) e = Entry(root, textvariable=sv) e.pack() root.mainloop() 但是

从这里的第一个答案: 当用户键入以下内容时,我可以调用callback:

from Tkinter import *

def callback(sv):
    print sv.get()

root = Tk()
sv = StringVar()
sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))
e = Entry(root, textvariable=sv)
e.pack()
root.mainloop() 

但是,事件发生在每个键入的字符上。当用户完成键入并按下enter键,或者输入窗口小部件失去焦点(即用户单击其他地方)时,如何调用事件?

我想这就是您想要的。我找到了相关信息。
bind
方法是关键

from Tkinter import *

def callback(sv):
    print sv.get()

root = Tk()

sv = StringVar()
e = Entry(root, textvariable=sv)
e.bind('<Return>', (lambda _: callback(e)))

e.pack()
root.mainloop() 
从Tkinter导入*
def回调(sv):
打印sv.get()
root=Tk()
sv=StringVar()
e=条目(根,textvariable=sv)
e、 绑定(“”,(lambda:回调(e)))
e、 包()
root.mainloop()
要捕获返回键按下事件,标准Tkinter功能会执行此操作。无需使用
StringVar

def callback(event):
  pass #do the work

e = Entry(root)
e.bind ("<Return">,callback)
def回调(事件): 通过做这项工作 e=条目(根) e、 绑定(“