基本上,我想运行一段代码,允许用户更改他们的凭据(在开始时已经给出了)。这里的函数-- def Uname_change()、def Password_change()、def PhoneNo_change()和def DOB_change()试图做到这一点。
但是这些函数中的变量'a‘(a是从各种entry小部件上的get()函数中获得的,如下所示)会产生一个空字符串。我不能理解为什么。
代码:
class ProfilePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg = 'light blue')
def show_profile():
def Uname_change():
Uname_change_window = tk.Tk()
label = tk.Label(Uname_change_window, text = 'Enter New UserName').pack()
NewUname = tk.StringVar()
entry = tk.Entry(Uname_change_window, textvariable = NewUname).pack()
confirm_button = tk.Button(Uname_change_window, text='Confirm', command= ).pack()
back_button = tk.Button(Uname_change_window, text='Back', command= lambda : Uname_change_window.destroy()).pack()
Uname_change_window.mainloop()
def Password_change():
Password_change_window = tk.Tk()
label = tk.Label(Password_change_window, text = 'Enter New Password').pack()
NewPassword = tk.StringVar()
entry = tk.Entry(Password_change_window, textvariable = NewPassword).pack()
def confirm():
a = NewPassword.get()
print(a)
#Write code for updation of database
Password_change_window.destroy()
confirm_button = tk.Button(Password_change_window, text='Confirm', command= confirm).pack()
back_button = tk.Button(Password_change_window, text='Back', command= lambda : Password_change_window.destroy()).pack()
def PhoneNo_change():
PhoneNo_change_window = tk.Tk()
label = tk.Label(PhoneNo_change_window, text = 'Enter New PhoneNumber').pack()
NewPhoneNo = tk.StringVar()
entry = tk.Entry(PhoneNo_change_window, textvariable = NewPhoneNo).pack()
def confirm():
a = NewPhoneNo.get()
print(a)
#Write code for updation of database
PhoneNo_change_window.destroy()
confirm_button = tk.Button(PhoneNo_change_window, text='Confirm', command= confirm).pack()
back_button = tk.Button(PhoneNo_change_window, text='Back', command= lambda : PhoneNo_change_window.destroy()).pack()
def DOB_change():
DOB_change_window = tk.Tk()
label = tk.Label(DOB_change_window, text = 'Enter New DateOfBirth').pack()
NewDOB = tk.StringVar()
entry = tk.Entry(DOB_change_window, textvariable = NewDOB).pack()
def confirm():
a = NewDOB.get()
print(a)
#Write code for updation of database
DOB_change_window.destroy()
confirm_button = tk.Button(DOB_change_window, text='Confirm', command= confirm).pack()
back_button = tk.Button(DOB_change_window, text='Back', command= lambda : DOB_change_window.destroy()).pack()
UserID_label = tk.Label(self, text = 'UserID').grid(row=2,column=0)
Uname_label = tk.Label(self, text = 'UserName').grid(row=3,column=0)
Uname_change_button = tk.Button(self, text = 'Edit', command = Uname_change).grid(row=3,column=2)#
Password_label = tk.Label(self, text = 'Password').grid(row=4,column=0)
Password_change_button = tk.Button(self, text = 'Edit', command = Password_change).grid(row=4,column=2)#
PhoneNo_label = tk.Label(self, text = 'PhoneNo').grid(row=5,column=0)
PhoneNo_change_button = tk.Button(self, text='Edit', command = PhoneNo_change).grid(row=5,column=2)#
DOB_label = tk.Label(self, text = 'DateOfBirth').grid(row=6,column=0)
DOB_change_button = tk.Button(self, text = 'Edit', command = DOB_change).grid(row=6,column=2)#
DOJ_label = tk.Label(self, text = 'DateOfJoining').grid(row=7,column=0)
show_button = tk.Button(self, text = 'Show', command = show_profile).grid(row=1,column=1)发布于 2020-10-31 03:46:10
问题的根源在于,您正在为entry小部件使用局部变量,并且您正在使用多个Tk实例。
如果您需要创建多个窗口,则应该对根窗口以外的每个窗口使用Toplevel而不是Tk。这是因为Tk的每个实例都有一个新的内部tcl解释器。一个窗口中的小部件和变量不能被另一个看到。
此外,由于您没有使用tkinter变量的任何功能(跟踪、在小部件之间共享),因此根本不需要使用它们。您可以简单地调用entry小部件的get方法。这解决了使用局部变量的问题,代码只需管理一个对象即可。
下面是我如何重写
def PhoneNo_change():
PhoneNo_change_window = tk.Toplevel()
label = tk.Label(PhoneNo_change_window, text = 'Enter New PhoneNumber')
entry = tk.Entry(PhoneNo_change_window)
label.pack()
entry.pack()
def confirm():
a = entry.get())
print(f"a is '{a}'")
.....。尽管老实说,您需要将所有重复的代码重构为一个函数。
https://stackoverflow.com/questions/64602743
复制相似问题