1

I'm trying to get a button to execute a function from another file called validate.py when it's pressed, but the function seems to be executing as soon as I run the code but not when I actually press it?

Button's code:

id_validate = ttk.Button(id_frame, text='Validate', 
command=validate.validate(pack_id.get()))

And this is the function's code:

def validate(pack_id):
    print('vAliDAte')
    return True

I want that function to execute every time I click the button and print 'validate' in the console, but it only executes once as soon as I run the code and doesn't respond when the button is pressed.

How do I stop it from executing when the code is run and only execute when the button is pressed?

1 Answers1

0

The function executes because you call it; ends the function name with parenthesis.

The command attribute only wants the name of the function.

However, you can fix this with lambda:

command=lambda: validate(pack_id.get())

A warning about using the name pack_id in the function: you already have used that name in the global scope in calling the function with the argument pack_id.get(). This might cause problems down the line.

figbeam
  • 7,001
  • 2
  • 12
  • 18