0

I write a simple lambda function callback when I enter something into entry with the following code:

self.myEntryWidget[0].trace('w', lambda frozen_command=self.getMiniumTokenReceive: frozen_command(b_index))

And the callback function is:

def getMiniumTokenReceive(self, b_index: int, *args):
    price = self.myEntryWidget[b_index].get()
    print(price)

But it gives an error

<lambda>() takes from 0 to 1 positional arguments but 3 were given.

Is there anyway that I can pass a function with parameters as a callback into my StringVar trace ?

Nice18
  • 476
  • 2
  • 12
  • When you `trace` a widget, tkinter passes 3 args by itself onto the callback. – Delrius Euphoria Jan 21 '22 at 15:04
  • A function passed to `.trace()` *will* be called with three parameters. You defined your lambda to have only one parameter - and you don't actually want that to actually receive a passed parameter, as it's just a trick to capture a value at definition time. So you need to use something like `lambda x, y, z, frozen_command=...` to accept and ignore those incoming parameters. – jasonharper Jan 21 '22 at 15:05
  • Try something like `def getMiniumTokenReceive(self, b_index: int)` and `lambda *args, frozen_command=self.getMiniumTokenReceive: frozen_command(b_index))` – Delrius Euphoria Jan 21 '22 at 15:08
  • See https://stackoverflow.com/q/29690463/7432 – Bryan Oakley Jan 21 '22 at 16:10
  • The solution by Delrius Euphoria work fine. Thanks – Hoàng Hiệp Jan 22 '22 at 01:51

0 Answers0