Function running without clicking the Python button

Asked

Viewed 325 times

0

Good night, My callbackInsereCritico function runs straight without the click of the button, which may be?

def callbackInsereCritico(nome):
  conn = psycopg2.connect(host='localhost',
                        database='diet+',
                        user='postgres',
                        password='teste')

  cur = conn.cursor()
  curCritics = conn.cursor()
  cur.execute("INSERT INTO critico (nome) VALUES ('" + nome + "')")                              
  conn.commit() 



rotuloAv = Label(formulario, text = "Cadastro do Avaliador")
rotuloNm = Label(formulario, text = "Nome:")
texto9 = Entry(formulario)
botaoAv =  Button(formulario, text = "Cadastrar",command = callbackInsereCritico(texto9.get())) 
resultadoAv = Label(formulario, text = None)

1 answer

0


You’re not passing a function the way you’re doing. Instead, you are running the function callbackInsereCritico and passing the return value to the attribute command. That’s why it’s running without the click event.

Imagine you have this function:

def fazalgo():
    return "fizalgo"

If you run:

type(fazalgo)

The comeback will be:

<type 'function'>

Now, if you execute:

type(fazalgo())

The comeback will be:

<type 'str'>

Did you notice the difference? In the first case, the function type received as parameter a function, already in the second, received the return of the function.

Now, as you need to pass an argument to the function, you can’t just do: command = callbackInsereCritico.

Has a pythonic way of doing that:

command = lambda: callbackInsereCritico(texto9.get())

lambda in Python is nothing more than an elegant way of declaring functions. They are very useful for certain moments, such as this.

So your code stays:

def callbackInsereCritico(nome):
  conn = psycopg2.connect(host='localhost',
                        database='diet+',
                        user='postgres',
                        password='teste')

  cur = conn.cursor()
  curCritics = conn.cursor()
  cur.execute("INSERT INTO critico (nome) VALUES ('" + nome + "')")                              
  conn.commit() 



rotuloAv = Label(formulario, text = "Cadastro do Avaliador")
rotuloNm = Label(formulario, text = "Nome:")
texto9 = Entry(formulario)
botaoAv =  Button(formulario, text = "Cadastrar", command = lambda: callbackInsereCritico(texto9.get()))
resultadoAv = Label(formulario, text = None)

How to explain lambda here departs from the scope of the question, I leave here this link: http://blog.alienretro.com/entendendo-python-lambda/

  • Hello, thank you for the answer! I believe I understood the functionality of lambda, but if I leave as you posted, it simply does not perform the function

  • Are you sure? Debugged? Some mistake or strange behavior?

  • Gave it right, did not run for a syntax error elsewhere in the code. So using lambda, solved the problem! Thank you very much beast!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.