AttributeError: 'str' object has no attribute 'get' when trying to get string information from a tkinter Entry Widget

AttributeError: 'str' object has no attribute 'get' when trying to get string information from a tkinter Entry Widget

我一直在为学校项目扩展编写这段代码,但似乎找不到解决方案。到目前为止,我一直在尝试让新用户注册系统正常工作,并且由于这个问题已经停止处理程序的登录部分。这是我的代码:

def newUser():
    usernameTemp = entryNew.get()
    passwordTemp = entryNewPass.get()

    database = {}

    addFile = open("database.txt", "a")
    addFile.write(usernameTemp + "," + passwordTemp + "\n")
    addFile.close()

def userValidation():
    global entryNew
    global entryNewPass
    global valid
    fileCheck = open("database.txt", "r")
    fileCheckData = fileCheck.read()
    fileCheck.close()

    entryNew = entryNew.get()
    entryNewPass = entryNewPass.get()

    database = {}

    for line in fileCheckData.splitlines():
        if line != '':           # ignore empty lines
            (username,password) = line.split(",", 1)
            database[username] = password
            if entryNew == username:
                Error(101)
                valid = False
            if valid == True:
                newUser()
                break

这里是 database.txt 文件:

######################,#############
testUser,password
admin,administratorPass

您在此处更改了 entryNewentryNewPass 变量:

global entryNew
global entryNewPass

entryNew = entryNew.get()
entryNewPass = entryNewPass.get()

您将这些声明为全局的,因此您将具有 .get() 方法(TkInter 输入框?)的对象替换为字符串。

为这些变量使用不同的名称:

entry = entryNew.get()

并在您的函数中使用这些新名称:

if entry == username:

您实际上并没有在代码中的任何地方使用 entryNewPass