In this tutorial we explore the Lambda functions, and how they can be used with the command option in Tkinter.

The basic idea behind the Lambda function is being able to create a nameless function within a single line. We will use this basic concept and create a single line function, which we can insert into the command option in many tkinter widgets, such as the button.

Why do we need Lambda in Tkinter?

Let’s take a look at a practical example where one would require the use of the Lambda function in Tkinter.

When linking a Tkinter widget to a function using the command option, you need to pass it’s name as shown in the below example.

def func():
    print("I have been clicked!")

button= tk.Button(frame, text= "Click Me!", command= func)

The reason why we don’t use the parenthesis (brackets), is because func() is a function call, whereas func is a reference to the function, which will be used to call it.

The problem arises when we want to call a function with some parameters. Since we cannot use the brackets, like func(param1), we need to come up with an alternate solution. And this is where Lambda functions come in.

Tkinter Lambda Functions

Let’s take a look at how we can implement the example from before using Lambda. Basically we “nest” the function we want to call, within the lambda function. So in essence, we clicking the button triggers the lambda function which triggers the actual function we meant to call.

def func():
    print("I have been clicked!")

button= Button(frame, text= "Click Me!", command= lambda: func())

You can try running the above code and it runs perfectly fine! But we aren’t taking advantage of lambda yet, so let’s do another example.

def func(name):
    print("Hello", name)

button= Button(frame, text= "Click Me!", command= lambda: func("John"))

In this above example we passed a parameter into the function within the lambda function. Unlike before this is a legal move, as we have nested functions.

Lambda with Parameters

In order to expand on the topic of lambda functions a bit, let’s take a look at some examples where we need to pass a parameter into the lambda function itself. Remember, a lambda function can also take parameters.

The example we will use is that of Tkinter key-binding, where we need to bind a certain action, such as clicking a button to a function that is called when the action is performed.

def HoverColor(widget, color, event)
    widget.config(fg = color)

button = tk.Button(self.frame, text = "Button")
button.bind('<Enter>', lambda event: HoverColor(button, "red", event))

The above example binds the button to the HoverColor function, which triggers when we move the mouse cursor over the button. Whenever a Tkinter event, occurs, it passes an event parameter which you can see in the above example.

This marks the end of the Tkinter Lambda Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Notify of
Inline Feedbacks
View all comments