简体   繁体   中英

When I click the “Change” button, the function “changeUsername” doesn't get executed?

How can I execute the function "changeUsername", when I click the "Change" button? Is there a fix for it? Thank you.

Here is the code:

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

You are reusing the name changeUsername , overwriting the function:

def changeUsername():
    ...

...

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

By the time changeusernamePop gets called, the name changeUsername refers to the Button instance, not the function you defined previously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM