Skip to main content
 首页 » 编程设计

Python,Kivy, "AssertionError: None is not callable"按钮调用函数时出错

2025年05月04日13pengyingh

所以我想使用这段代码在按下按钮时调用一个函数:

botao_ok.bind(on_press=f_adicionar_socios(txt_n_socio.text,txt_nome.text,txt_filho_de.text,txt_filho_e_de.text,txt_data_nas.text,txt_tipo_ID.text,txt_num_ID.text,txt_NIF.text,txt_morada_rua.text,txt_morada_localidade.text,txt_codigo_postal.text,txt_tel_fixo.text,txt_telemovel.text,txt_email.text,txt_tipo_socio.text,txt_data_admicao.text,txt_zona.text,txt_actividade.text,txt_actividade_de.text,txt_actividade_ate.text,txt_observacoes.text)) 

但是为了简单起见,我只需要解决这个问题:

#My Function 
def teste_(nome): 
    print nome 
#Button 
botao_ok.bind(on_press=teste_('Ola')) 
# Button is inside a Class MYApp 

它给出错误:AssertionError: None is not callable

我已经尝试了所有我遇到的困难,但无法解决这个问题...... 谢谢

请您参考如下方法:

当你编写 teste_('Ola') 函数运行并返回 None

所以当你写的时候

botao_ok.bind(on_press=teste_('Ola')) 

它实际上被设置为:

botao_ok.bind(on_press=None) 

简而言之,这是导致您遇到问题的原因。

为了让它在按下按钮时调用 teste_('Ola'),您可以使用 lambda 函数:

botao_ok.bind(on_press=lambda x:teste_('Ola'))