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.

Answer from Delrius Euphoria on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lambda.asp
Python Lambda
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... A lambda function is a small anonymous function....
๐ŸŒ
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

Understanding Python Lambda behavior with Tkinter Button - Stack Overflow
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate] (5 answers) Closed 4 years ago. I would like to understand how a button is working using lambda. I have the following Python code: More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Using the lambda function in 'command = ' from Tkinter. - Stack Overflow
Traceback (most recent call last): ...s/3.4/lib/python3.4/tkinter/init.py", line 1533, in call return self.func(*args) TypeError: () missing 3 required positional arguments: 'evt', 'Current_Weight', and 'entree1' ... I thought the lambda function allows us to uses some args in a event-dependant function. ... The command lambda does ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What exactly is "lambda" in Python? - Stack Overflow
Lambda is an anonymous function in Python programming language, instead of bearing a def statement in the front it is simply called and written lambda. ... Exactly same you can do with a lambda function. ... # returns 4 For more details you can look up in this blog post. http://friendlypython.herokuapp.com/2017/1/anonymous-function-lambda/ or here ... It's an inline anonymous function. ... assign_command ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Tkinter: pass reference to self to lambda function?
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! ๐Ÿ™๐Ÿป More on reddit.com
๐ŸŒ r/learnpython
3
1
May 12, 2021
๐ŸŒ
Real Python
realpython.com โ€บ python-lambda
How to Use Python Lambda Functions โ€“ Real Python
December 1, 2023 - In this step-by-step tutorial, you'll learn about Python lambda functions. You'll see how they compare with regular functions and how you can use them in accordance with best practices.
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).

๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ lambda
Python lambda function - creating anonymous functions in Python
Python lambda function can be used in GUI programming with Tkinter. It allows to create small, inline functions for the command parameter.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ using-lambda-in-gui-programs-in-python
Using lambda in GUI programs in Python - GeeksforGeeks
July 23, 2025 - 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): print(value) # update Label text in GUI output_label["text"] = value if __name__ == "__main__": root = Tk() root.title("GFG") root.geometry("800x400") # GUI Heading Label(root, text="Using lambda in GUI programs in Python", font="calibre 28 bold", padx=5, pady=15).pack() # Button Button(root, text="Click me to Greet", command=lambda: greet( "Hello User"), bd=5, fg="blue", font="calibre 18 bold").pack() output_label = Label( root, text="", font="calibre 28 bold", padx=5, pady=15) output_label.pack() root.mainloop()
Find elsewhere
๐ŸŒ
Python Programming
pythonprogramming.net โ€บ passing-functions-parameters-tkinter-using-lambda
Python Programming Tutorials
class StartPage(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self,parent) label = tk.Label(self, text="Start Page", font=LARGE_FONT) label.pack(pady=10,padx=10) button = tk.Button(self, text="Visit Page 1", command=lambda: qf("Check me out, I'm passing vars!")) button.pack()
๐ŸŒ
Amazon Web Services
docs.aws.amazon.com โ€บ aws lambda โ€บ developer guide โ€บ building lambda functions with python
Building Lambda functions with Python - AWS Lambda
Runtime: Choose Python 3.14. Choose Create function. The console creates a Lambda function with a single source file named lambda_function. You can edit this file and add more files in the built-in code editor. In the DEPLOY section, choose Deploy to update your function's code.
๐ŸŒ
Python 101
python101.pythonlibrary.org โ€บ chapter26_lambda.html
Chapter 26 - The lambda โ€” Python 101 1.0 documentation
Weโ€™ll start with Tkinter since itโ€™s included with the standard Python package. Hereโ€™s a really simple script with three buttons, two of which are bound to their event handler using a lambda: import Tkinter as tk class App: """""" def __init__(self, parent): """Constructor""" frame = tk.Frame(parent) frame.pack() btn22 = tk.Button(frame, text="22", command=lambda: self.printNum(22)) btn22.pack(side=tk.LEFT) btn44 = tk.Button(frame, text="44", command=lambda: self.printNum(44)) btn44.pack(side=tk.LEFT) quitBtn = tk.Button(frame, text="QUIT", fg="red", command=frame.quit) quitBtn.pack(side=tk.LEFT) def printNum(self, num): """""" print("You pressed the %s button" % num) if __name__ == "__main__": root = tk.Tk() app = App(root) root.mainloop()
๐ŸŒ
CodersLegacy
coderslegacy.com โ€บ home โ€บ python โ€บ using lambda with โ€˜commandโ€™ in tkinter
Using lambda with 'command' in Tkinter - CodersLegacy
January 25, 2022 - 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())
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-lambda-functions
Python Lambda Functions โ€“ How to Use Anonymous Functions with Examples
February 24, 2023 - For example, in the following code, a button click event is handled using a lambda function in Tkinter (a GUI programming toolkit for Python): import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click Me!", command=lambda: print("Button clicked!")) button.pack() root.mainloop() In this example, we use the lambda function lambda: print("Button clicked!") as the command argument of the Button widget in Tkinter.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
In this example, a lambda function is defined to convert a string to its upper case using upper(). ... In Python, lambda functions are created using the lambda keyword.
Published ย  3 weeks ago
๐ŸŒ
Amazon Web Services
docs.aws.amazon.com โ€บ aws lambda โ€บ developer guide โ€บ building lambda functions with python โ€บ define lambda function handler in python
Define Lambda function handler in Python - AWS Lambda
January 31, 2026 - The Lambda function handler is the method in your Python code that processes events. When your function is invoked, Lambda runs the handler method.
๐ŸŒ
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.

๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ operators โ€บ lambda.html
lambda โ€” Python Reference (The Right Way) 0.1 documentation
>>> # this is a code snippet from a Tkinter gui app >>> # in this case lambda is quite convenient >>> self.btn_cancel = Button(self.progress_container, text='Cancel', >>> command=lambda: subprocess.call('taskkill /f /im uberzip.exe', >>> shell=True)) #TODO
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-lambda-function-explained
How the Python Lambda Function Works โ€“ Explained with Examples
December 17, 2024 - As I explained above, the lambda function does not have a return keyword. As a result, it will return the result of the expression on its own. The x in it also serves as a placeholder for the value to be passed into the expression.
๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ tutorial-lambda-functions-in-python
Tutorial: Lambda Functions in Python
March 6, 2023 - We use a lambda function to evaluate only one short expression (ideally, a single-line) and only once, meaning that we aren't going to apply this function later. Usually, we pass a lambda function as an argument to a higher-order function (the ...