按钮点击时,Tkinter无法获取该功能的值

时间:2018-04-06 20:52:46

标签: python tkinter tkinter-entry

因此,我尝试从输入(3)获取值,并在按钮单击时将它们传递给函数。

这就是我现在所拥有的:

Label(self.master, text="Name").grid(row=0, sticky=W)
    Label(self.master, text="Username").grid(row=1, sticky=W)
    Label(self.master, text="Email").grid(row=2, sticky=W)

    self.e_name = Entry(self.master).grid(row=0, column=1)
    self.e_username = Entry(self.master).grid(row=1, column=1)
    self.e_email = Entry(self.master).grid(row=2, column=1)

    Button(self.master, text="Login", command=self.login_client).grid(row=3, column=1, sticky=E)

功能:

def login_client(self):
    print(self.e_name.get())

现在,我收到此错误:AttributeError: 'NoneType' object has no attribute 'get' ...

可以在此处找到该文件的完整代码:https://gist.github.com/RosiersRobin/343c0194fde2e8e3184f24cb5aecac28

我只想在用户login_client

中给出用户的输入

1 个答案:

答案 0 :(得分:2)

您的问题是.grid()来电。网格返回None,因此self.e_name将为None。只需将这些行改为这些行:

self.e_name = Entry(self.master)
self.e_name.grid(row=0, column=1)
self.e_username = Entry(self.master)
self.e_username.grid(row=1, column=1)
self.e_email = Entry(self.master)
self.e_email.grid(row=2, column=1)