【问题标题】:Python: <lambda> TypeError: 'NoneType' object is not callablePython:<lambda> TypeError:“NoneType”对象不可调用
【发布时间】:2015-03-19 20:32:10
【问题描述】:

我正在尝试从另一个名为 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)

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

【问题讨论】:

    标签: python tkinter nonetype


    【解决方案1】:

    您将None 分配给Add_userttk.Button.grid() 返回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)
    

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2012-04-28
      • 2021-08-18
      相关资源
      最近更新 更多