当我点击 "Change" 按钮时,函数 "changeUsername" 没有被执行?
When I click the "Change" button, the function "changeUsername" doesn't get executed?
如何在单击 "Change" 按钮时执行函数 "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()
您正在重用名称 changeUsername
,覆盖函数:
def changeUsername():
...
...
changeUsername = Button(...) # no more function
调用 changeusernamePop
时,名称 changeUsername
指的是 Button
实例,而不是您之前定义的函数。
如何在单击 "Change" 按钮时执行函数 "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()
您正在重用名称 changeUsername
,覆盖函数:
def changeUsername():
...
...
changeUsername = Button(...) # no more function
调用 changeusernamePop
时,名称 changeUsername
指的是 Button
实例,而不是您之前定义的函数。