当我单击“更改”按钮时,函数“changeUsername”不会被执行?

时间:2018-05-14 15:06:06

标签: python button tkinter toplevel

单击“更改”按钮后,如何执行“changeUsername”功能?它有解决方法吗?谢谢。

以下是代码:

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()

1 个答案:

答案 0 :(得分:1)

您正在重复使用名称changeUsername,覆盖该功能:

def changeUsername():
    ...

...

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

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