Python: <lambda> TypeError: 'NoneType' object is not callable

Python: <lambda> TypeError: 'NoneType' object is not callable

我正在尝试从另一个名为 Edit_user_admin 的函数调用一个名为 Add_user 的函数,我很确定我写的所有内容都是正确的,但我总是遇到同样的错误。

  File "G:/PVH_work/PVH_program/ParkTheReal.py", line 395, in <lambda>
    Add_user = ttk.Button(frame_27, text="Add User", command=lambda: Add_user(frame_27, data_dictionary)).grid(row=1, column=0)
  TypeError: 'NoneType' object is not callable

这里是函数 Edit_user_admin:

def Edit_user_admin(form_item, data_dictionary, row_num):
    form_item.grid_forget()
    frame_27 = Frame(gui)
    frame_27.grid()

    MyProfile = ttk.Button(frame_27, text="My profile", command=lambda: My_profile_admin(frame_27, data_dictionary, row_num)).grid(row=0, column=0)
    TrainingRecord = ttk.Button(frame_27, text="Training Record", command=lambda: Training_record_admin(frame_27, data_dictionary, row_num)).grid(row=0, column=1)
    Compare = ttk.Button(frame_27, text="Compare", command=lambda: Compare_admin(frame_27, data_dictionary, row_num)).grid(row=0, column=2)
    EditUsers = ttk.Button(frame_27, text="Edit Users", command=lambda: Edit_user_admin(frame_27, data_dictionary, row_num)).grid(row=0, column=3)
    Team = ttk.Button(frame_27, text="View/Edit Team", command=lambda: Team_admin(frame_27, data_dictionary, row_num)).grid(row=0, column=4)
    Logout = ttk.Button(frame_27, text="Logout", command=lambda: Logout(frame_27)).grid(row=0, column=5)

    Add_user = ttk.Button(frame_27, text="Add User", command=lambda: Add_user(frame_27, data_dictionary, row_num)).grid(row=1, column=0)
    Edit_user = ttk.Button(frame_27, text="Edit User", command=lambda: Edit_user(frame_27, data_dictionary, row_num)).grid(row=1, column=1)
    Remove_user = ttk.Button(frame_27, text="Remove User", command=lambda: Remove_user(frame_27, data_dictionary, row_num)).grid(row=1, column=2)

这里是函数 Add_user:

def Add_user(form_item, data_dictionary, row_num):
    form_item.grid_forget()
    frame_28 = Frame(gui)
    frame_28.grid()

    #Declare variables for creating a new user account
    __Username  = StringVar()
    __Name      = StringVar()
    __Age       = StringVar()
    __Email     = StringVar()
    __DoB       = StringVar()



    MyProfile = ttk.Button(frame_28, text="My profile", command=lambda: My_profile_admin(frame_28, data_dictionary, row_num)).grid(row=0, column=0)
    TrainingRecord = ttk.Button(frame_28, text="Training Record", command=lambda: Training_record_admin(frame_28, data_dictionary, row_num)).grid(row=0, column=1)
    Compare = ttk.Button(frame_28, text="Compare", command=lambda: Compare_admin(frame_28, data_dictionary, row_num)).grid(row=0, column=2)
    EditUsers = ttk.Button(frame_28, text="Edit Users", command=lambda: Edit_user_admin(frame_28, data_dictionary, row_num)).grid(row=0, column=3)
    Team = ttk.Button(frame_28, text="View/Edit Team", command=lambda: Team_admin(frame_28, data_dictionary, row_num)).grid(row=0, column=4)
    Logout = ttk.Button(frame_28, text="Logout", command=lambda: Logout_so(frame_28)).grid(row=0, column=5)

我在其他函数上遇到过这个错误,但我发现在我尝试调用的函数的名称中添加一个“_”,然后在命令中添加相同的扩展名。

您已将 None 分配给 Add_userttk.Button.grid() returns None:

Add_user = ttk.Button(...).grid(row=1, column=0)

您应该对按钮引用和函数使用相同的名称; Python 在这种情况下将使用局部变量,而不是全局函数。

使用不同的名称,并分别调用.grid() :

add_user_button = ttk.Button(
    frame_27, text="Add User", 
    command=lambda: Add_user(frame_27, data_dictionary, row_num))
add_user_button.grid(row=1, column=0)

同样适用于其他按钮。

但是,如果您没有在其他任何地方使用 add_user_button 引用,您可以将其设为一行,但您不必费心分配结果:

ttk.Button(
    frame_27, text="Add User", 
    command=lambda: Add_user(frame_27, data_dictionary, row_num)
).grid(row=1, column=0)