u/socal_nerdtastic and u/Spataner Thank you both for very detailed and enlightening replies. I can see my thinking was a little unclear in more than one aspect so back to the drawing board armed with some good knowledge! Answer from neilbaldwn on reddit.com
🌐
Python Programming
pythonprogramming.net › passing-functions-parameters-tkinter-using-lambda
Passing functions with Parameters in Tkinter using Lambda
Command is where you can pass functions, but here we're going to pass a lambda function instead, which creates a quick throwaway function, using the parameters we've set. Here's what you should get: If you click the button, you should get a print out to the console with your message. In case you are confused or lost, here's the current code in full up to this point: # The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter # License: http://creativecommons.org/licenses/by-sa/3.0/ import tkinter as tk LARGE_FONT= ("Verdana",
🌐
TutorialsPoint
tutorialspoint.com › tkinter-button-commands-with-lambda-in-python
Tkinter button commands with lambda in Python
March 5, 2021 - #Import the library from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win= Tk() #Set the window geometry win.geometry("750x250") #Display a Label def print_text(text): Label(win, text=text,font=('Helvetica 13 bold')).pack() btn1= ttk.Button(win, text="Button1" ,command= lambda: print_text("Button 1")) btn1.pack(pady=10) btn2= ttk.Button(win, text="Button2" ,command= lambda: print_text("Button 2")) btn2.pack(pady=10) btn3= ttk.Button(win, text="Button3" ,command= lambda: print_text("Button 3")) btn3.pack(pady=10) win.mainloop()
Discussions

python - Using the lambda function in 'command = ' from Tkinter. - Stack Overflow
See similar questions with these tags. ... New site design and philosophy for Stack Overflow: Starting February 24, 2026... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... ... 0 Trying to declare a lambda function inside command parameter for the button but it doesn't work(Tkinter... More on stackoverflow.com
🌐 stackoverflow.com
python - Tkinter: Why does the lambda function allow me to use arguments in the command parameter? - Stack Overflow
I assume you mean something like ... by Tkinter, will call foo(3). Otherwise, you are setting the return value of foo(3) as the the callback command. ... No, maybe I didn't express myself correctly. I just want to understand what is happening when I use the lambda functions with command parameter... More on stackoverflow.com
🌐 stackoverflow.com
January 10, 2020
Understanding Python Lambda behavior with Tkinter Button - Stack Overflow
I would like to understand how a button is working using lambda. I have the following Python code: from tkinter import * def comando_click(mensagem): print(mensagem) menu_inicial = Tk() More on stackoverflow.com
🌐 stackoverflow.com
Tkinter button command using lambda to call a class method - how???
command=self.open_notebook will work, and is a better than using lambda in my opinion. You also need to move most of the code under def __init__ rather than where it is. class MainWidget: def __init__(self): root = Tk() root.geometry("400x400") search_frame = Frame(root) search_frame.pack() self.search_function = SearchFunction(search_frame) open_notebook_button = Button(root, text="Open", command=self.open_notebook) open_notebook_button.pack() root.mainloop() def open_notebook(self): self.search_function.get_emp_id() # and more stuff to add later More on reddit.com
🌐 r/Tkinter
4
4
September 11, 2022
🌐
Reddit
reddit.com › r/learnpython › tkinter: pass reference to self to lambda function?
r/learnpython on Reddit: Tkinter: pass reference to self to lambda function?
May 12, 2021 -

Early days on Tkinter for me and I'm struggling with something.

I have several buttons that I want to all call the same function when clicked but then determine what task to perform within that function based on which of the buttons was actually clicked.

One way I managed it was to pass a string to the lambda function e.g.

button.command=lambda clickFunction('red')

then:

def clickFunction(buttonColor):

etc.

but is there a way I can pass 'self' in the lambda function so that I could do it a different way e.g.

button.color = 'red'

button.command=lamda clickFunction(self)

then:

def clickFunction(self):

print("Button colour is " + self.color

this doesn't work but it can be sort of made to work by passing the button itself:

button.command=lambda clickFunction(button)

but I don't necessarily want to do that and am curious if there's a way to pass 'self' to the function instead as that strikes me as more flexible (especially as I'm trying to write a class to create buttons by passing a set of parameters to the button constructor so that when I have many buttons to create I don't have to type the same button construction code out multiple times. Forgive me if this approach is wrong too! 🤣

I think I've read several hundred articles and Stackoverflow posts now but I can't seem to find the answer.

🌐
CodersLegacy
coderslegacy.com › home › python › using lambda with ‘command’ in tkinter
Using lambda with 'command' in Tkinter - CodersLegacy
January 25, 2022 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › using-lambda-in-gui-programs-in-python
Using lambda in GUI programs in Python - GeeksforGeeks
April 28, 2025 - Use the lambda function in the command parameter of the button. ... Button(root,text="Click me to Greet",command=lambda : greet("Hello User"),bd=5,fg="blue",font="calibre 18 bold").pack() ... from tkinter import * # functions def greet(value): ...
🌐
ZetCode
zetcode.com › python › lambda
Python lambda function - creating anonymous functions in Python
The functions take the lambda function as the second parameter. The lambdas return the attribute of the object on which the min, max functions operate. $ ./mmfun.py Car(name='Skoda', price=9000) Car(name='Bentley', price=350000) Python lambda function can be used in GUI programming with Tkinter.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 59686792 › tkinter-why-does-the-lambda-function-allow-me-to-use-arguments-in-the-command-p
python - Tkinter: Why does the lambda function allow me to use arguments in the command parameter? - Stack Overflow
January 10, 2020 - I assume you mean something like command=lambda: foo(3) rather than command=foo(3). The lambda expression creates a function that, when called by Tkinter, will call foo(3). Otherwise, you are setting the return value of foo(3) as the the callback ...
🌐
Python 101
python101.pythonlibrary.org › chapter26_lambda.html
Chapter 26 - The lambda — Python 101 1.0 documentation
We create a tk.Button instance here and bind to our printNum method in one fell swoop. The lambda is assigned to the button’s command parameter. What this means is that we’re creating a one-off function for the command, much like in the quit button where we call the frame’s quit method.
Top answer
1 of 3
1

When you use () with a function name(func(args)), then it is immediately calling/invoking the function while python is executing the line, you do not want that. You want to ONLY call the function when the button is clicked. tkinter will internally call the function for you, all you have to do is give the function name.

Why use lambda? Think of it as a function that returns another function, your code can be lengthened to:

func  = lambda: comando_click("Nova_Mensagem")
botao = Button(menu_inicial, text = "Executar", command=func)

func is the function name and if you want to call it, you would say func(). And when you say command=comando_click("Nova_Mensagem") then command has the value returned by command click(because you call the function with ()), which is None and if I'm not wrong, if the given value is None, it will not be called by tkinter. Hence your function is executed just once because of () and as a result of calling the function, you are assigning the value of the function call(None) before the event loop starts processing the events.

Some other methods:

  • Using partial from functools:
from functools import partial

botao = Button(.....,command=partial(comando_click,"Nova_Mensagem"))
  • Using a helper function:
def helper(args):
    def comando_click():
        print(args)

    return comando_click

botao = Button(...., command=helper("Nova_Mensagem"))

IMO, lambdas are the easiest way to proceed with calling a function with arguments.

2 of 3
1

In this code:

command=comando_click("Nova_Mensagem")

you have called the comando_click function, once, and assigned the result (None) to the command argument. Nothing will happen when command is called (in fact you should get a TypeError exception because None is not callable).

In this code:

command=lambda:comando_click("Nova_Mensagem")

you have not actually called comando_click yet -- you have created a new function (using lambda) that will in turn call comando_click when it is called. Every time the button is clicked, your new function will get called.

If the lambda is confusing, you can do the exact same thing with a def like this:

def button_command():
    comando_click("Nova_Mensagem")

...

command=button_command  # no ()!  we don't want to actually call it yet!

The lambda expression is just an alternative to using def when you want to create a small single-use function that doesn't need a name (e.g. you want to make a function that calls another function with a specific argument, exactly as you're doing here).

🌐
Python Forum
python-forum.io › thread-36121.html
Tkinter and lambda
I'm trying to make a button with a simple function with a variable defined in another function. I want the program to check if the key the user entered is in a list or not, and show a message box accordingly, using get(). It shows error even if the k...
🌐
Python Tutorial
pythontutorial.net › home › tkinter tutorial › tkinter command binding
Tkinter Command Binding - Python Tutorial
April 3, 2025 - When you click a button, the lambda expression associated with the button’s command will execute. It’ll be called the select() function with a string argument.
🌐
Reddit
reddit.com › r/tkinter › tkinter button command using lambda to call a class method - how???
r/Tkinter on Reddit: Tkinter button command using lambda to call a class method - how???
September 11, 2022 -

I'm stuck, what am I missing?

Not sure if it's classes or tkinter that I don't correctly understand.

If I run the example below and hit the button I get "missing argument (self)". I totally get that.

class MainWidget:
    root = Tk()
    root.geometry("400x400")

    def open_notebook(self):
        self.search_function.get_emp_id()
        # and more stuff to add later

    search_frame = Frame(root)
    search_frame.pack()
    search_function = SearchFunction(search_frame)

    open_notebook_button = Button(root, text="Open", command=open_notebook)
    open_notebook_button.pack()

    root.mainloop()

Then I tried:

command=lambda: open_notebook()

... but it doesn't know open_notebook.

command=lambda: self.open_notebook()

... it doesn't know self

command=lambda: root.open_notebook()

... and it doesn't know root.

As I am playing around more with this I realize I have no idea if I maybe need a contructor and what difference exactly that would make, what goes in it (no pun intended) and what doesn't. I have no experience with OOP beyond the very very basics.

I'm grateful for any advice!

🌐
Delft Stack
delftstack.com › home › howto › python tkinter › how to pass arguments to tkinter button command
How to Pass Arguments to Tkinter Button Command | Delft Stack
February 2, 2024 - You could also use Python lambda operator or function to create a temporary, one-time simple function to be called when the Button is clicked. from sys import version_info if version_info.major == 2: import Tkinter as tk elif version_info.major == 3: import tkinter as tk app = tk.Tk() labelExample = tk.Button(app, text="0") def change_label_number(num): counter = int(str(labelExample["text"])) counter += num labelExample.config(text=str(counter)) buttonExample = tk.Button( app, text="Increase", width=30, command=lambda: change_label_number(2) ) buttonExample.pack() labelExample.pack() app.mainloop()
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-pass-arguments-to-tkinter-button-command
How to Pass Arguments to Tkinter Button Command? - GeeksforGeeks
October 3, 2022 - In some situations, it's necessary to supply parameters to the connected command function. In this case, the procedures for both approaches are identical; the only thing that has to vary is the order in which you use them.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to utilize tkinter button commands with lambda in python
5 Best Ways to Utilize tkinter Button Commands with Lambda in Python - Be on the Right Side of Change
March 6, 2024 - It shows how to give fixed parameters to the function via button commands without invoking the function prematurely. With lambda functions, not only can you pass static arguments but also dynamic ones that may change over the runtime of the ...
🌐
Python
mail.python.org › pipermail › tkinter-discuss › 2004-September › 000194.html
[Tkinter-discuss] passing parameters to a lambda function in Tkinter menubar
September 25, 2004 - if __name__ == '__main__': root = Tkinter.Tk() Pmw.initialise(root) root.title(title) exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) exitButton.pack(side = 'bottom') widget = Demo(root) root.mainloop()
🌐
CopyProgramming
copyprogramming.com › howto › python-tkinter-button-command-lambda-pass-variable
Python Tkinter Button Command Lambda: Pass Variable, Variable Scope, and 2026 Best Practices - Python tkinter button command lambda pass variable
December 18, 2025 - Use default parameters like lambda x=i: to capture the value at creation time. ... A: Lambda is more flexible for most tkinter use cases, especially when arguments come from widgets. Use partial when binding arguments to existing functions or when you prefer its syntax.
Top answer
1 of 2
2

Can anyone explain to me the importance of the lambda function when creating interface with Tkinter?

Arguably, they aren't important at all. They are just a tool, one of several that can be used when binding widgets to functions.

The problem with the binding in your question is due to the fact that when you use bind to bind an event to a function, tkinter will automatically pass an event object to that function you must define a function that accepts that object.

This is where lambda comes in. The command needs to be a callable. One form of a callable is simply a reference to a function such as the one you're using (eg: command=self.concluir_return). If you don't want to modify your function to accept the parameter you can use lambda to create an anonymous function -- a callable without a name.

So, for your specific case, you can define a lambda that accepts the argument, and then the lambda can call your function without the argument.

But all was solved when I looked into web and modified the line of code with the lambda function.

self.master.bind("<Return>", lambda event: self.concluir_return())

This works because the code is effectively the same as if you did this:

def i_dont_care_what_the_name_is(event):
    self.concluir_return()
self.master.bind("<Return>", i_dont_care_what_the_name_is)

As you can see, lamda isn't required, it's just a convenient tool that lets you create a simple function on the fly that calls another function.

2 of 2
1

The bind method takes two arguments, sequence and handler, and will call f(event) when the specified event occurs.

In your case, concluir_return wasn't expecting any argument other than self, so your code raised an error when it was called with event.

The lambda function you used is the equivalent of:

def f(event):
    return concluir_handler()

so it bypasses the problem by just ignoring the event argument.

Another way of doing this would be to add an argument to concluir_return.