0

单击“更改”按钮时,如何执行“更改用户名”功能?有解决办法吗?谢谢你。

这是代码:

teacherSetScreen = Tk()
teacherSetScreen.title("Teacher Home")
teacherSetScreen.geometry("300x130")

newUsername = StringVar(teacherSetScreen)

def changeUsername():
    print("dgdfgf")
    newName = newUsername.get() 
    if(validateUsername(newName)):
        print("Username Changed!")
        updateData("Username", newName)
    else:
        print("Invalid Username")

def changeUsernamePop():
    popup = Toplevel()
    labelNewUsername = Label(popup, text="New Username: ")
    labelNewUsername.grid(row=0, column=0, pady=4)
    entryNewUsername = Entry(popup, textvariable=newUsername)
    entryNewUsername.grid(row=0, column=1)
    buttonNewUsername = Button(popup, text="Change", command=changeUsername)
    buttonNewUsername.grid(row=1, column=0, columnspan=3, padx=50)

labelUsername = Label(teacherSetScreen, text="Username: "+labelText["Username"])
labelUsername.grid(row=1, column=0, pady=4, columnspan=2, sticky=W)
changeUsername = Button(teacherSetScreen, text="Change Username", command=changeUsernamePop)
changeUsername.grid(row=1, column=3, pady=4, columnspan=3)

teacherSetScreen.mainloop()
4

1 回答 1

1

您正在重用 name changeUsername,覆盖函数:

def changeUsername():
    ...

...

changeUsername = Button(...)  # no more function

changeusernamePop被调用时,名称changeUsername指的是Button实例,而不是您之前定义的函数。

于 2018-05-14T15:10:46.760 回答