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
  • 13,796
  • 25
  • 99
  • 185
thenickname
  • 6,226
  • 13
  • 40
  • 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
  • 104,129
  • 18
  • 164
  • 224
  • 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,242
  • 3
  • 35
  • 58