【问题标题】:Why is there an en in this answer?为什么这个答案中有一个 en ?
【发布时间】:2018-11-25 15:29:26
【问题描述】:

一个没有类的简单例子:

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()

以上是这个问题的答案:Why is Tkinter Entry's get function returning nothing?

所以我检查了代码并且它工作正常。但是 - 我不明白为什么“return_entry”括号中有一个“en”。代码的任何其他部分都没有提到它,所以我认为它是语法——但是为了什么?

如果我没有看到 OP 的帐户已停用 2 年,我会在上述答案上发表评论

【问题讨论】:

  • bind 必须期望给它的函数接受一个参数;在这种情况下,大概是Entry。或者你在问什么是函数参数?
  • 大概是打错了;参数名为en,但代码引用entry“我认为是语法” - 你是什么意思
  • 看起来没有任何作用。我同意@jonsharpe 这很可能是一个错字
  • 啊。谢谢。我对函数参数有一个粗略的概念,例如调用这样的函数: A_function(y=1) A_function(y) 但我从来不知道它可以以这种方式使用
  • @jonrsharpe 我的意思是 Carcigenicate 所说的。好吧-随时将您的 cmets 放入答案中,让我关闭此

标签: python tkinter syntax


【解决方案1】:

通过bind绑定到widget的回调函数接受一个事件对象作为参数。

我将您的打印声明修改如下:

 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

【讨论】:

  • 虽然它不需要修改 - 它按我的预期工作。无论如何-谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2018-09-08
  • 2021-11-18
  • 2020-02-15
  • 2010-09-07
  • 2017-07-07
相关资源
最近更新 更多