3

For some reason, this Button is automatically calling bot_analysis_frame without the button being pressed. I'm guessing it's because the command is a function with arguments.

Is there a way to have the button only call this function and pass the required variables only upon being pressed?

Button(topAnalysisFrame, text='OK', command=bot_analysis_frame(eventConditionL, eventBreakL)).pack(side=LEFT)
nbro
  • 15,395
  • 32
  • 113
  • 196
thenickname
  • 6,684
  • 14
  • 41
  • 42
  • Possible duplicate of [Why is Button parameter “command” executed when declared?](http://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – nbro Mar 10 '17 at 10:52

2 Answers2

14

Read the section here on passing callbacks.

You are storing the result of that function to the command argument and not the function itself.

I believe this:

command = lambda: bot_analysis_frame(eventConditionL,eventBreakL)

might work for you.

Mark
  • 106,305
  • 20
  • 172
  • 230
  • 2
    Note that closures created in a loop will not work as "expected" (it actually works correctly, yu just have to get that closures really capture variables, not values). All the functions created in `[lambda: print(i) for i in range(5)]` will print the final value of `i`, 4. You can hack around this by default arguments, which are bound at definition time: `lambda i=i: print(i)` will work. –  Dec 06 '10 at 19:48
2

I'm pretty sure this has been answered before. Instead of this:

Button(topAnalysisFrame,
       text='OK',
       command=bot_analysis_frame(eventConditionL,eventBreakL)).pack(side=LEFT)

You could use lambda like so:

Button(topAnalysisFrame,
       text="OK",
       command=lambda: bot_analysis_frame(eventConditionL, eventBreakL)).pack(side=LEFT)
jgritty
  • 11,660
  • 3
  • 38
  • 60