【问题标题】:Why is the .get() function of tkinter returing an empty string为什么 tkinter 的 .get() 函数返回一个空字符串
【发布时间】:2020-10-31 03:46:09
【问题描述】:

基本上我想运行一个允许用户更改其凭据的代码(已经在开始时给出)。在这里,函数 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)

【问题讨论】:

  • 请尽量减少此代码,并使其运行。就目前而言,存在一些语法错误。如果您从多个函数中看到相同的行为,就本问题而言,我们只需要一个函数。

标签: tkinter tkinter-entry


【解决方案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}'")
        ...

...虽然老实说,您需要将所有重复的代码重构为一个函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2019-03-12
    相关资源
    最近更新 更多