1

I am trying to create a virtual keyboard using Tkinter and I am trying to do something analogous to the code supplied below. I am having trouble with understanding the lambda construct in the code below, which from what I understand is the only way to go without having to write a button widget for every single letter that exists on the keyboard. I have always understood that when you are using lambda, you do something analogous to:

variable = lambda x: x+5

but in the supplied code further down, the Button widget has a lambda I have not seen before written like this:

command=lambda value=text: select(entry, value)

I have spent all day reading about lambda, and I still cannot get this.

This is the code link:

How to call and close a virtual keyboard made by Tkinter using touchscreen display

Specifically this is the line I am having problems with:

tk.Button(window, text=text, width=width,
          command=lambda value=text: select(entry, value),
          padx=3, pady=3, bd=12, bg="black", fg="white", takefocus = False
         ).grid(row=y, column=x, columnspan=columnspan)
martineau
  • 119,623
  • 25
  • 170
  • 301
Agent π
  • 33
  • 1
  • 8
  • Does this answer your question? https://stackoverflow.com/a/59687539/7432 Just think of `lambda` as a `def` without a name. That's not precisely correct, but conceptually it's close enough. – Bryan Oakley Dec 15 '21 at 17:37
  • the lambda in this case is weird and i was hopping someone would explain to me what on earht is going on here, this is not how you write lamdas. – Agent π Dec 15 '21 at 18:53
  • This is fairly common technique in tkinter apps. `lambda`s create functions and functions can have both regular arguments and keyword arguments. The example is using the latter and it doing so to "capture" the value of `text` at the moment the anonymous function is created (so latter changes to it won't affect the value being passed to the function. It's not the only way to do something like this as illustrated in a section titled [The extra arguments trick](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/extra-args.html) of an online tkinter programming reference. – martineau Dec 15 '21 at 19:28
  • 1
    A `lambda` is **never** the only way to go. `lambda value=text: select(entry, value)` is just the same as using a full function definition like the following: `def _func(value=text): return select(entry, value)` – juanpa.arrivillaga Dec 15 '21 at 19:49
  • 1
    @diggusbickus well, no, it is a hack precisely to avoid the semantics of late-binding lexical scope (a closure would *already* be created, but the problem is that it is lexically scoped, of course, you can use *another* layer to fix that variable) – juanpa.arrivillaga Dec 15 '21 at 19:50
  • 1
    @Agentπ what do you mean "this is not how you write lambdas", it is a *perfectly normal* lambda expression. What exactly is it you don't understand about it? Do you understand what default arguments are? Is that what is confusing you? – juanpa.arrivillaga Dec 15 '21 at 19:52
  • yes the default argument was confusing me thanks LOL...sorted it out now. – Agent π Dec 15 '21 at 22:09

1 Answers1

0

My problem was that i did not know you could use the default argument on lambda....i am surprised not many picked up how dumb i was though.

command=lambda value=text: select(entry, value)

value or value=text is the same thing if text is a variable. That was my problem

Agent π
  • 33
  • 1
  • 8
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '21 at 04:32