The command lambda does not take any arguments at all; furthermore there is no evt that you can catch. A lambda can refer to variables outside it; this is called a closure. Thus your button code should be:

bouton1 = Button(main_window, text="Enter",
    command = lambda: get(Current_Weight, entree1))

And your get should say:

def get(loot, entree):
    loot = float(entree.get())
    print(loot)
Answer from Antti Haapala on Stack Overflow
🌐
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.
Discussions

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
python - What's the importance of lambda functions in tkinter? - Stack Overflow
Can anyone explain to me the importance of the lambda function when creating interface with Tkinter? I am building a super simple interface to get familiar with Tkinter and I wanted to make so that... 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
python - Using lambda function in command of Button widget in tkinter - Stack Overflow
I'm trying to create simple GUI like this: three set of label, entry, and READ button, one set for one row when READ button is pressed, the value of entry will be displayed on label. But all Read ... More on stackoverflow.com
🌐 stackoverflow.com
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 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:
🌐
TutorialsPoint
tutorialspoint.com › tkinter-button-commands-with-lambda-in-python
Tkinter button commands with lambda in Python
March 5, 2021 - The button command is defined with the lambda function to pass the specific value to a callback function. #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()
🌐
CodersLegacy
coderslegacy.com › home › python › using lambda with ‘command’ in tkinter
Using lambda with 'command' in Tkinter - CodersLegacy
January 25, 2022 - 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, ...
Find elsewhere
🌐
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.

🌐
GeeksforGeeks
geeksforgeeks.org › using-lambda-in-gui-programs-in-python
Using lambda in GUI programs in Python - GeeksforGeeks
April 28, 2025 - 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()
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - UI frameworks like Tkinter, wxPython, or .NET Windows Forms with IronPython take advantage of lambda functions for mapping actions in response to UI events.
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.

🌐
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!

🌐
Stack Overflow
stackoverflow.com › questions › 66073980 › using-lambda-function-in-command-of-button-widget-in-tkinter
python - Using lambda function in command of Button widget in tkinter - Stack Overflow
Something like this : lambda x=label[j], y=entry[j]: x.set(y.get()) Here I first set label[j] to x and entry[j] to y and then change the values inside lambda. import tkinter as tk main = tk.Tk() label = [None]*3 entry = [None]*3 read = [None]*3 for j in range(3): label[j] = tk.StringVar() tk.Label(main, textvariable = label[j], relief = 'raised', width = 7).place(x = 5, y = 40+30*j) entry[j] = tk.Entry(main, width=8) entry[j].place(x=80, y=40 + 30 * j) read[j] = tk.Button(main, text="READ", pady=0, padx=10, command= lambda x=label[j], y=entry[j]: x.set(y.get())) read[j].place(x=150, y=40 + 30 * j) main.mainloop()
🌐
CodeSpeedy
codespeedy.com › home › using lambda in gui programs in python
Using lambda in GUI programs in Python - CodeSpeedy
October 9, 2019 - Otherwise, we have to use function with parameters which cannot be directly placed at the command key word. This is where we take advantage of lambda. Now see this code with lambda that acts as both up and down counter when we click the respective buttons and the count is displayed on a label. Look, how much interactive we can make a program with widgets. import tkinter window = tkinter.Tk() counter = tkinter.IntVar() counter.set(0) def click(var, value): var.set(var.get() + value) frame = tkinter.Frame(window) frame.pack() button = tkinter.Button(frame, text='Up', command=lambda: click(counter, 1)) button.pack() button = tkinter.Button(frame, text='Down', command=lambda: click(counter, -1)) button.pack() label = tkinter.Label(frame, textvariable=counter) label.pack() window.mainloop()
🌐
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...
🌐
Sololearn
sololearn.com › en › Discuss › 2642566 › python-tkinter-lambda
Python tkinter lambda | Sololearn: Learn to code for FREE!
So I created a function: def updateEntry(nr, tableName, entryList): print(nr, tableName) for E in entryList: print(E) The entrys and buttons are created in a loop: while x < len(Database-entrys): entry = Entry(second_frame) .... button = Button(second_frame, command= updateEntry(nr, tableName, entryList) But the button always showed the last number - no problem: I added lambda: button = Button(..., command = lambda nr = nr, tableName = tableName, entryList = entryList: updateEntry(nr, tableName, entryList), which worked, but only for the nr!
Top answer
1 of 2
2

The command argument requires a function with no positional arguments, so using lambda x: <do-something> will raise an error. In this case none of the arguments need to be passed during the callback and so you should simplify things to

def show_description():
    key = int(code_entry.get())
    if key in dictio:
        textEntry.set(dictio[key])
    else:
        messagebox.showinfo("Info", "The number is not in the database")

show_button = Button(root, text="Mostrar descripción", command=show_description)

Also, doing this

dictio[int(code_entry.get())]

the way you did could have raised a KeyError after fixing the lambda having no arguments.

2 of 2
0
from tkinter import *
from tkinter import messagebox


root=Tk()

dictio={1:"one", 2:"two"}

test=False
textEntry = StringVar() 
display=StringVar()
def show_description():
    print(display.get()) #==get the value of the stringvat
    x=int(display.get()) #==convert the value to integer
    if dictio.get(x)==None:
        messagebox.showinfo("Info", "The number is not in the database")
    else:
        textEntry.set(dictio.get(x)) #==get the value of the key from dictio dictionary and set it for description_entry
code_entry = Entry(root,textvariable=display)
display.set("") #==set value as nothing
code_entry.grid(row=1, column=1, sticky='nsew')
description_entry = Entry(root, state="disabled", textvariable = textEntry)
description_entry.grid(row=0, column=1, sticky='nsew')

show_button = Button(root, text="Mostrar descripción", command=show_description)


show_button.grid(row=0, column=2)
root.mainloop()
🌐
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 - With lambda functions, not only can you pass static arguments but also dynamic ones that may change over the runtime of the application. This is particularly useful for interactive applications that depend on user input or other real-time data ...
🌐
Python Tutorial
pythontutorial.net › home › tkinter tutorial › tkinter command binding
Tkinter Command Binding - Python Tutorial
April 3, 2025 - def callback_function(args): # do somethingCode language: Python (python) Then, define a lambda expression and assign it to the command argument of the button widget.