简体   繁体   中英

Python, Kivy, "AssertionError: None is not callable" Error on function call by button

So I whant to use this code to call a function on a button press:

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))

But to keep it simple, I only need to solve this problem:

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

and it gives the error: AssertionError: None is not callable

Ive tryied everything I tough off and can't solve this... Thank you

When you write teste_('Ola') the function runs and returns None

So when you write

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

It actually gets set to:

botao_ok.bind(on_press=None)

Which in short is causing your problem.

In order to get it to call teste_('Ola') When the button is pressed, you could use a lambda function:

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

In case your function call doesn't require any arguments, you can write the name of the function without parentheses or a lambda:

function_caller.bind(on_press=your_function)

this works because python considers functions as first-class values, just like a string, an integer or a float.

(this answer is here for the people that have a similar problem and want cleaner code, the correct answer in this exact situation is what Vincent described above)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM