一个没有类的简单示例:
from tkinter import *
master = Tk()
# Create this method before you create the entry
def return_entry(en):
"""Gets and prints the content of the entry"""
content = entry.get()
print(content)
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
entry.bind('<Return>', return_entry)
mainloop()
以上是这个问题的答案:为什么 Tkinter Entry 的 get 函数什么都不返回?
所以我检查了代码并且它起作用了。但是 - 我不明白为什么“return_entry”括号中有一个“en”。代码的任何其他部分都没有提到它,所以我认为它是语法 - 但为了什么?
如果我没有看到 OP 的帐户已停用 2 年,我就会对上述答案发表评论
通过 bind 绑定到小部件的回调函数将事件对象作为参数。
我将您的打印语句修改如下:
print("Event Object: ", en.widget, en, "\nResult: ", content)
在创建的窗口中键入“Test{Return}”后,将生成以下内容:
Event Object: .!entry <KeyPress event keysym=Return keycode=13 char='\r' x=52 y=8>
Result: Test
事件对象可用于确定需要什么处理。在您的情况下,它不相关,但回调机制需要它。该事件发生在 .!entry 小部件中,是一个 KeyPress 事件,显示了其他特征。
HTH
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句