Button starts command without clicked

Ich have a form, everthings fine. I have a function filling the form from a file does what expected to do.

Now I created a button that should call the function if pressed.
But if I start the program, the function is called at once. Using PyCharm as IDE, find the function name gives me 2 results, the function definition and the command at the button. If I set a debugger breakpoint on the function it is reach without the button is pressed
The button definition:

read_button = ttk.Button(data_frame,text="Read",command=fill_form())
read_button.grid(row=row_nr,column=3,padx=pad_x,pady=pad_y)
root.mainloop()

before the buttons are about 460 lines of working code creating a 188 fields form with a lot of functions creating lables, entries, combo boxed etc. including the working “fill_form”

Need help, hint

Regards Rainer

1 Like
read_button = ttk.Button(data_frame,text="Read",command=fill_form())
#                                                                ^^

The () parentheses here mean that the function is called when the button is defined. Remove the parentheses to pass the function itself.

3 Likes

Thanks for quick help, it works now. But as much as I think, I can not imagine what that will be meaningfull, to start a fing while defineing a button. What will that button then be good for. And what if I want to gif a parameter to the function when button is pressed. OK im still a novize in Python, but not by far in programming

Regards

To pass a parameter to the function, you could use something like functools.partial or a lambda.

1 Like