我正在为tkinter创建用户登录/注册模块并正在测试它但是当我尝试通过将两个字符串传递给用户字典来登录时出现此错误:
TypeError: argument of type 'instance' is not iterable
为什么会这样?我认为这两个变量都是字符串......
以下是模块代码:
import pickle
from Tkinter import *
class LoginBox:
def __init__(self, parent):
Label(parent, text="Username:").grid()
self.ubox=Entry(parent)
self.ubox.grid()
Label(parent, text="Password:").grid()
self.pbox=Entry(parent, show="*")
self.pbox.grid()
def check(self, userlist):
pswrd=self.pbox.get()
uname=self.ubox.get()
if uname in userlist and userlist[uname] == pswrd:
return True
else:
tkMessageBox.showinfo("Incorrect", "Incorrect username or password")
self.pbox.delete(0, "end")
class RegisterBox:
def __init__(self, parent):
Label(parent, text="Username:").grid()
self.uname=Entry(parent)
self.uname.grid()
Label(parent, text="Password:").grid()
self.pswrd=Entry(parent, show="*")
self.pswrd.grid()
def check(self, userlist):
pswrd=self.pswrd.get()
uname=self.uname.get()
if uname in userlist.users and userlist.users[uname] == pswrd:
return False
else:
userlist.adduser(uname, pswrd)
class UserList:
def __init__(self):
self.users={}
def adduser(self, user, pswrd):
self.users[user] = pswrd
def deluser(user):
del self.users[user]
这是我的应用代码:
import tkMessageBox
from Tkinter import *
import loginutility as lu
root=Tk()
def check():
if logbox.check() == True:
tkMessageBox.showinfo("Success", "Good job!")
users=lu.UserList()
regbox=lu.RegisterBox(root)
Button(root, text="Register", command=lambda: regbox.check(users)).grid()
logbox=lu.LoginBox(root)
Button(root, text="Login", command=check).grid()
root.mainloop()
答案 0 :(得分:1)
您在RegisterBox
和LoginBox
中以不同方式检查用户并登录。
userlist
不是普通词典,而是UserList
类,您必须在userlist.users
中使用userlist.users[uname]
和LoginBox.check()
。
-
btw:你可以创建UserList.check(user,password)