tkinter - 为什么 tkinter 的 .get() 函数返回一个空字符串
问题描述
基本上我想运行一个允许用户更改其凭据的代码(已经在开始时给出)。在这里,函数 def Uname_change()、def Password_change()、def PhoneNo_change() 和 def DOB_change() 试图做到这一点。
但是每个函数中的变量“a”(a 是从各种条目小部件上的 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)
解决方案
问题的根源在于您为条目小部件使用了局部变量,并且您使用了多个 Tk 实例。
如果您需要创建多个窗口,则不应Toplevel
为Tk
除根窗口以外的每个窗口使用。那是因为每个实例Tk
都有一个新的内部 tcl 解释器。来自一个的小部件和变量不能被另一个看到。
此外,由于您没有使用 tkinter 变量的任何功能(跟踪、小部件之间的共享),因此您根本不需要使用它们。您可以简单地调用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}'")
...
...虽然老实说,您需要将所有重复的代码重构为一个函数。
推荐阅读
- c++ - C++ 数组计算器
- google-signin - 是否违反谷歌政策?
- android - Flutter 问题:ListView.Builder 中的 DropDown
- android - 如何在 Android Jetpack Compose 中更改 OutlineTextField 边框宽度?
- flutter - 在颤动中获取 hls url 的视频编解码器错误
- python - 如何对齐/合并 2 个列表?(Python)
- javascript - 事件“OnKeyDown”仅在反应应用程序中触发一次
- firebase - Flutter firebase auth web不持久
- java - 如何在android 11中实现存储权限。有人可以分享一个用java实现的例子
- python - 如何将 MYSQL 转换为 sqlalchemy 烧瓶