Tkinter:我不能使用“get”函数从条目小部件获取信息

2024-09-27 09:33:43 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某种原因,我不能再使用.get()函数了

这是我的密码:

import tkinter
from tkinter import *


def submit():
    file = open('database.txt', 'w')
    file.write('Username:', un, '\nPassword:', pw)
    file.close()

wn = Tk()
wn.geometry('500x200')

Label(text='Geben Sie hier Ihren Benutzername ein:').pack()
username = Entry().pack()

Label(text='\n').pack()

Label(text='Geben Sie hier Ihr Passwort ein:').pack()
password = Entry().pack()

Label(text='\n').pack()


un = str(username.get())
pw = str(password.get())

btn = Button(text='Submit', command=submit).pack()


wn.mainloop()

过去它很管用,所以我真的不知道该怎么办

我得到的错误如下所示:

un = str(username.get()) AttributeError: 'NoneType' object has no attribute 'get'


Tags: textimportgettkinterusernamelabelpackfile
1条回答
网友
1楼 · 发布于 2024-09-27 09:33:43

代码中的变量username、password和btn并不像您想象的那样是Entry()Entry()Button()对象,而是pack()方法的返回(似乎它不返回任何东西,所以所有这些都是非类型对象)

如果您打算在代码中以Entry()Button()对象的形式访问这些对象,则应将initiation和pack()分为两行,如下所示:

username = Entry()
username.pack()

password = Entry()
password.pack()

btn = Button(text='Submit', command=submit)
btn.pack()

相关问题 更多 >

    热门问题