'instance'类型的字典错误参数不可迭代

时间:2016-01-23 01:49:47

标签: python dictionary tkinter

我正在为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()

1 个答案:

答案 0 :(得分:1)

您在RegisterBoxLoginBox中以不同方式检查用户并登录。

userlist不是普通词典,而是UserList类,您必须在userlist.users中使用userlist.users[uname]LoginBox.check()

-

btw:你可以创建UserList.check(user,password)