Short answer: you cannot. Callbacks can't return anything because there's nowhere to return it to -- except the event loop, which doesn't do anything with return values.

In an event based application, what you typically will do is set an attribute on a class. Or, if you're a beginner, you can set a global variable. Using a global variable isn't a good idea for real code that has to be maintained over time but it's OK for experimentation.

So, for example, since C appears to be a global variable in your example, you would do something like:

def button():
    global C
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    C = A
Answer from Bryan Oakley on Stack Overflow
๐ŸŒ
Python Programming
pythonprogramming.net โ€บ passing-functions-parameters-tkinter-using-lambda
Passing functions with Parameters in Tkinter using Lambda
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()
Discussions

python - Returning values in lambda function while using Tkinter button - Stack Overflow
0 Trying to declare a lambda function inside command parameter for the button but it doesn't work(Tkinter) 0 Python tkinter return value from function call in a button More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
lambda tkinter not. able to return value from button callback - Stack Overflow
I am trying to get a return value from a button using lambda but the value returned is not getting updated. Can someone else help in finding the reason, I am new to python and may be facing an obvi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python 3.x - How to return value from tkinter button callback - Stack Overflow
I have a function that has a tkinter window defined and run inside of it. There is a button in that window, and I need the button to execute a return statement and return a value for the function. ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ getting the return value of a lambda function called from tkinter button
r/learnpython on Reddit: Getting the return value of a lambda function called from tkinter button
May 12, 2018 -

Alright, so I am creating a small GUI program using tkinter, and one of the functions it performs requires a user to enter a document number. It then makes changes to the database based on the number entered, so I want to make sure the user entered it correctly.

To accomplish this, I am asking the user to enter the number again in a pop up window and then I compare the values and return True if they match or False otherwise.

I am having a hard time getting my def: to return the result of the lambda function inside of it.

I've tried assigning the lambda expression to a variable, but that seems to not return True or False as expected, but rather some internal Python stuff (memory addresses and such).

Am I trying to do this the right way? Is there a simpler way?

My code

๐ŸŒ
Python
python-list.python.narkive.com โ€บ oKfJuiPk โ€บ returning-values-from-a-lambda
Returning values from a lambda
But I need to use a lambda in order to pass arguments to the function. return a+b, a*b x = a+b y = a*b Indeed I could do it like that, but I would need to define globals for x and y. Can this be done without using globals ? from Tkinter import * root = Tk() btn = Button(root, text='Press Me', command=lambda a=2, b=5: operations(a,b)) btn.pack() With the function I've defined, things should work.
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).

๐ŸŒ
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()
Find elsewhere
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ tkinter tutorial โ€บ tkinter command binding
Tkinter Command Binding - Python Tutorial
April 3, 2025 - import tkinter as tk from tkinter import ttk root = tk.Tk() def button_clicked(): print('Button clicked') button = ttk.Button(root, text='Click Me', command=button_clicked) button.pack() root.mainloop()Code language: Python (python) If you want to pass the arguments to a callback function, you can use a lambda expression.
๐ŸŒ
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...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 68232494 โ€บ lambda-tkinter-not-able-to-return-value-from-button-callback
lambda tkinter not. able to return value from button callback - Stack Overflow
Instead of returning value from function, make the variable clk or CLOCK (whichever you are updating) as global, and then in function change_direction_dummy() you can set the value of the global variable ...
๐ŸŒ
Quora
quora.com โ€บ How-can-you-return-a-value-from-a-function-that-is-executed-from-the-Tkinter-button-Python-TkInter-development
How to return a value from a function that is executed from the Tkinter button (Python, TkInter, development) - Quora
In Tkinter, a button command is a callback executed by the GUI event loop; its return value is discarded by Tkinter. To "get" a value produced by that callback you must store it somewhere accessible and then read that storage from the rest of ...
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ lambda
Python lambda function - creating anonymous functions in Python
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. It allows to create small, inline functions for the command ...
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ tkinter-button-command-return-value
Python: Is it possible to obtain a return value from the command of a Tkinter Button?
April 1, 2023 - There are two ways to do it: 1) a global variable 2) to store the variable you want to return in another class: 1) define the function you use in a button: def yourFunction (x): global variab variab = x. Transfer the function to your button: button = Button (root, command=lambda: yourFunction (x)) ...
๐ŸŒ
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
import tkinter as tk main = tk.Tk() label = [None]*3 entry = [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) tk.Button(main, text="READ", pady=0, padx=10, command= lambda: label[j].set(entry[j].get())).place(x=150, y=40 + 30 * j) main.mainloop()
๐ŸŒ
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!