-1

So I'm working on, AGAIN the code in This Question...
so im wondering how to go about doing this:

from tkinter import *
from tkinter import messagebox as mb
# ...
def info(text):
    mb.showinfo(text)
# ...
helpmenu.add_command(label="Version", command=info("Not yet realesed"))
# ...

what it does is automaticly executes info().
how can I prevent this?

Khristos
  • 973
  • 2
  • 11
  • 22
Bloxy Craft
  • 131
  • 1
  • 3
  • 12

1 Answers1

2

You need to use a lambda function to prevent the info() from being executed automatically:

helpmenu.add_command(label="Version", command=lambda: info("Not yet realesed")) 
Khristos
  • 973
  • 2
  • 11
  • 22
  • 1
    I will complement answer: It is assumed that the command argument of the `add_command` method is a function that will be called for a particular action. When you write `command = info ("Not yet realesed") `you do not pass the function, but the result of calling the function as an argument. This is the essence of the problem. The solution is to wrap a call of `info("Not yet realesed")` with a lambda(as it done by @Khristos), or to make a function with a 'bound' argument using `partial` from `functools`. – Grigory Sep 14 '17 at 06:25
  • He shouldn't need to use a lambda, he should be able to pass the function directly to the argument as grigoriy stated (using partial from functools). – Charles D Pantoga Sep 14 '17 at 06:28