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
🌐
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 ...
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).

Discussions

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
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
list - tkinter button commands with lambda in Python - Stack Overflow
ive tried searching for a solution but couldn't find one that works. I have a 2d list of tkinter Buttons, and i want to change their Text when it is clicked by the mouse. I tried doing this: def 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
🌐
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.
🌐
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()
🌐
CodersLegacy
coderslegacy.com › home › python › using lambda with ‘command’ in tkinter
Using lambda with 'command' in Tkinter - CodersLegacy
January 25, 2022 - 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. 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.
🌐
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!

Find elsewhere
🌐
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()
🌐
ZetCode
zetcode.com › python › lambda
Python lambda function - creating anonymous functions in Python
#!/usr/bin/python from tkinter import Tk, BOTH, messagebox from tkinter.ttk import Frame, Button class Example(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.initUI() def initUI(self): self.parent.title("Buttons") self.pack(fill=BOTH, expand=1) btn1 = Button(self, text="Button 1", command=lambda: self.onClick("Button 1")) btn1.pack(padx=5, pady=5) btn2 = Button(self, text="Button 2", command=lambda: self.onClick("Button 2")) btn2.pack(padx=5, pady=5) btn2 = Button(self, text="Button 3", command=lambda: self.onClick("Button 3")) btn2.pack(padx=5, pady=5) def onClick(self, text): messagebox.showinfo("Button label", text); def main(): root = Tk() root.geometry("250x150+300+300") app = Example(root) root.mainloop() if __name__ == '__main__': main()
🌐
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...
🌐
CodeSpeedy
codespeedy.com › home › using lambda in gui programs in python
Using lambda in GUI programs in Python - CodeSpeedy
October 9, 2019 - 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() This code creates a no argument lambda function to pass into each button just where it’s needed. The lambda functions then pass the values into click function. You can learn: Tkinter pack(), grid() Method In Python
🌐
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.org
discuss.python.org › python help
Suggested solutions for Tkinter loop + command not working as expected - Python Help - Discussions on Python.org
December 22, 2022 - I’m having issues using lambda functions to dynamically create commands for my Tkinter GUI, and the suggested solutions I’ve found on SO don’t seem to work as expected. The objective of this simple code is to create a GUI that should have two buttons, one that says “Buy Apple” and ...
🌐
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.
🌐
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
An easy fix would be to manually set label[j] and entry[j] to some other variable, then apply the command. 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 ...
🌐
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 - Passing arguments to Tkinter button command could be implemented with partial object from functools module, or with lambda function.
🌐
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 ...