if username.get() == '2021160138' and password.get() == 'aa': window_2() else: print("登录失败,用户名或密码错误。")报错'NoneType' object has no attribute 'get'怎么改
时间: 2023-12-15 20:05:40 浏览: 19
这个错误是因为你的 `username` 或者 `password` 对象为空,无法调用 `get()` 方法。你需要在调用 `get()` 方法之前,先对它们进行判断,确保它们不为空。
你可以使用以下代码来进行判断和修改:
```python
if username is not None and password is not None:
if username.get() == '2021160138' and password.get() == 'aa':
window_2()
else:
print("登录失败,用户名或密码错误。")
else:
print("输入框为空。")
```
这个代码会先判断 `username` 和 `password` 是否为空,如果不为空则继续判断用户名和密码是否正确。如果为空,则输出 "输入框为空" 的提示信息。
相关问题
username=tk.StringVar() password=tk.StringVar() # 创建标签和输入框组件 label_username =tk.Label(window_1,text="用户名").place(relx=0.25,rely=0.25,width=40,height=20) entry_username =tk.Entry(window_1).place(relx=0.35,rely=0.25,width=150,height=20) label_password =tk.Label(window_1,text="密码").place(relx=0.25,rely=0.35,width=40,height=20) entry_password =tk.Entry(window_1,show="*").place(relx=0.35,rely=0.35,width=150,height=20) def login(): #username = entry_username #password = entry_password username = entry_username.get() password = entry_password.get() # 验证用户名和密码是否正确,如果正确则进行登录操作 if username == '2021160138' and password == 'aa': window_2() else: print("登录失败,用户名或密码错误。") b1=tk.Button(window_1,text='登陆',command=login).place(relx=0.25,rely=0.5,width=40,height=20) b2=tk.Button(window_1,text='退出',command=login).place(relx=0.55,rely=0.5,width=40,height=20)报错'NoneType' object has no attribute 'get'怎么改
这个错误是因为你在创建 `entry_username` 和 `entry_password` 时,使用了 `place()` 方法,该方法返回的是 `None` 而不是 `Entry` 对象,因此无法调用 `get()` 方法。
你需要将 `place()` 方法调用与 `Entry` 对象的创建分开,如下所示:
```python
entry_username = tk.Entry(window_1)
entry_username.place(relx=0.35, rely=0.25, width=150, height=20)
entry_password = tk.Entry(window_1, show="*")
entry_password.place(relx=0.35, rely=0.35, width=150, height=20)
```
这样你就可以正确地获取输入框中的值了。
# 用户登录函数 def user_login(username_entry, password_entry): username = username_entry.get() password = password_entry.get() # 验证用户名和密码 if username == "樊晓盟" and password == "111": user_menu() else: tk.messagebox.showinfo("登录失败", "用户名或密码错误")
感谢提供代码。根据你的实现,`user_login()` 函数接受 `username_entry` 和 `password_entry` 作为参数,并从中获取用户名和密码。
在验证用户名和密码后,你可以根据需要执行不同的操作。在这个例子中,如果用户名和密码正确,则调用 `user_menu()` 函数;否则,弹出一个消息框显示登录失败的信息。
请确保在调用 `user_login()` 函数时正确传递了 `username_entry` 和 `password_entry` 参数,并确保这两个参数是正确的 `Entry` 对象。
如果你遇到任何问题,请提供更多的代码或错误信息,我将尽力帮助你解决问题。