This should work:

...
btnaText='ButtonA'
btna = Button(root, text = btnaText, command = lambda: sayHi(btnaText))
btna.pack()

For more information take a look at Tkinter Callbacks

Answer from Ocaso Protal on Stack Overflow
🌐
Python Tutorial
pythontutorial.net › home › tkinter tutorial › tkinter command binding
Tkinter Command Binding - Python Tutorial
April 3, 2025 - First, define a function that accepts the args argument: def callback_function(args): # do somethingCode language: Python (python)
People also ask

What is the fundamental requirement for a Tkinter widget command
ANS: The command option expects a reference to a callable object (a function name) that, when invoked by the widget, takes zero arguments.
🌐
sqlpey.com
sqlpey.com › python › tkinter-passing-arguments-to-callbacks
Tkinter Callbacks: Passing Arguments to Widget Command Options
Does the button command callback return values
ANS: No, the callback assigned to the command attribute does not return data to Tkinter in a usable way. State changes must be managed by modifying external objects or instance attributes.
🌐
sqlpey.com
sqlpey.com › python › tkinter-button-command-arguments-techniques
Tkinter Button Command Arguments: Techniques for Passing Data to ...
Is using lambda cleaner than functools partial
ANS: This is subjective; lambda avoids an import, while partial can offer clearer syntax for complex argument passing setups.
🌐
sqlpey.com
sqlpey.com › python › tkinter-passing-arguments-to-callbacks
Tkinter Callbacks: Passing Arguments to Widget Command Options
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-pass-arguments-to-tkinter-button-s-callback-command
How can I pass arguments to Tkinter button's callback command?
We will pass the label as the argument in the button command by using lambda function. #Import necessary Library from tkinter import * from tkinter import ttk #Create an instance of tkinter window win= Tk() #Set the geometry of tkinter window win.geometry("750x250") #Define the function to change the value in label widget def change_text(label): label.configure(text= "Hey, I am Label-2", background="gray91") #Create a Label label = Label(win, text= "Hey, I am Label-1", font= ('Helvetica 15 underline'), background="gray76") label.pack(pady=20) #Create a button btn= ttk.Button(win, text= "Change", command= lambda:change_text(label)) btn.pack(pady=10) win.mainloop()
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch09s02.html
Avoiding lambda in Writing Callback Functions - Python Cookbook [Book]
July 19, 2002 - import Tkinter def hello(name): print "Hello", name root = Tk( ) # the lambda way of doing it: Button(root, text="Guido", command=lambda name="Guido": hello(name)).pack( ) # using the Command class: Button(root, text="Guido", command=Command(hello, "Guido")).pack( ) Of course, you can also use a more general currying approach, which lets you fix some of the arguments when you bind the callback, while others may be given at call time (see Recipe 15.8).
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
TutorialsPoint
tutorialspoint.com › construct-button-callbacks-with-tkinter
Construct button callbacks with Tkinter
February 15, 2024 - When working with button callbacks, it's crucial to anticipate and handle potential errors gracefully. The use of try-except blocks is an effective strategy · import tkinter as tk from tkinter import messagebox def on_button_click(): try: print("Button clicked!") except Exception as e: messagebox.showerror("Error", f"An error occurred: {str(e)}") root = tk.Tk() root.title("Error Handling in Callbacks") root.geometry("720x250") button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=10) root.mainloop()
🌐
sqlpey
sqlpey.com › python › tkinter-passing-arguments-to-callbacks
Tkinter Callbacks: Passing Arguments to Widget Command Options
October 29, 2025 - This example demonstrates a callback that alters the button’s own text upon being pressed, utilizing a global counter. import tkinter as tk i = 0 BUTTON_PHRASES = ("First Press", "Second Attempt", "Success", "Done") def update_button_text(): global i # Access the button object (if globally defined) or modify external state btn['text'] = BUTTON_PHRASES[i % len(BUTTON_PHRASES)] i = (i + 1) print(f"Counter: {i}") main_window = tk.Tk() main_window.title("Stateful Callback Demo") btn = tk.Button(main_window, text="Initial State", command=update_button_text) btn.pack(padx=20, pady=20) main_window.mainloop()
🌐
Python Examples
pythonexamples.org › python-tkinter-pass-arguments-to-command-function-when-clicks-on-menu-item
Tkinter - Pass arguments to the Command Function when user clicks on Menu Item
When user clicks on the first item Item_1, we call item_action("Item 1"). Similarly, when user clicks on the second item Item_2, we call item_action("Item 2"). We are setting the same function item_action() as callback function for the menu items, but the argument we pass changes with the menu item.
Find elsewhere
🌐
Python Programming
pythonprogramming.net › passing-functions-parameters-tkinter-using-lambda
Passing functions with Parameters in Tkinter using Lambda
# 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", 12) class SeaofBTCapp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) container = tk.Frame(self) container.pack(side="top", fill="both", expand = True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} frame = StartPage(container, self) self.frames[StartPage] = frame frame.grid(row
🌐
sqlpey
sqlpey.com › python › tkinter-button-command-arguments-techniques
Tkinter Button Command Arguments: Techniques for Passing Data to Callbacks
October 29, 2025 - The partial function from Python’s standard functools library allows you to create a new callable object where some arguments of an existing function are pre-filled. This results in a callable object suitable for the command attribute. ... from functools import partial import tkinter as tk def target_operation(a, b, c): result = a * b + c print(f"Calculated Result: {result}") root = tk.Tk() main_frame = tk.Frame(root) main_frame.pack() arg1, arg2, arg3 = 5, 10, 3 # Create the partial object operation_with_args = partial(target_operation, arg1, arg2, arg3) # Assign the partial object to the command action_btn = tk.Button(main_frame, text='Run Partial', command=operation_with_args) action_btn.pack(pady=10) root.mainloop()
🌐
StackHowTo
stackhowto.com › home › python › tkinter › how to pass arguments to tkinter button’s callback command?
How to Pass Arguments to Tkinter button's callback Command? - StackHowTo
January 13, 2022 - In this tutorial, we are going to see how to pass arguments to Tkinter button’s callback Command? The ‘command’ option of the Button Tkinter widget is triggered when the user presses the button. In some cases, you want to pass arguments to the function associated with the ‘command’ option, but you cannot pass arguments as below: btn = tk.Button(gui, text="Click here!", command=myFunction(arguments)) We will see how to pass arguments to the action associated with a Button using lambda functions in Python.
Top answer
1 of 3
11

The notion of "returning" values from callbacks doesn't make sense in the context of an event driven program. Callbacks are called as the result of an event, so there's nowhere to return a value to.

As a general rule of thumb, your callbacks should always call a function, rather than using functools.partial or lambda. Those two are fine when needed, but if you're using an object-oriented style of coding they are often unnecessary, and lead to code that is more difficult to maintain than it needs to be.

For example:

def compute():
    value = var.get()
    result = square(value)
    list_of_results.append(result)

button = Tk.Button(root, text='click', command = compute)
...

This becomes much easier, and you can avoid global variables, if you create your application as a class:

class App(...):
    ...
    def compute():
        ...
        result = self.square(self.var.get())
        self.results.append(result)
2 of 3
9

Sorry for being 6 years late, but recently I figured out a good way to do this without making your code messy and hard to maintain. This is pretty much what DaveTheScientist has said, but I just want to expand on it a little. Usually, in Tkinter, if you want to have a button call a function, you do the following:

exampleButton = Button(root, text = 'Example', command = someFunc)

This will simply call someFunc whenever the button is pressed. If this function, however, takes arguments, you need to use lambdas and do something like this:

exampleButton = Button(root, text = 'Example', command = lambda: someFunc(arg1, arg2))

The above line of code will run someFunc and use the variables arg1 and arg2 as arguments for that function. Now, what you could do in a program where, a lot of the times, you would need the functions run by buttons to return values, is create a new function which is called by every button.

This function takes the function you want your button to run as a first argument, and that function's arguments afterwards.

def buttonpress(function, *args):
    value = function(*args)

Then when you create the button, you do:

exampleButton = Button(root, text = 'Example', command = lambda: buttonpress( someFunc, arg1, arg2 ))

This will run the given function (in this case, someFunc) and store the returned value in the value variable. It also has the advantage that you can use as many arguments as you want for the function your button runs.

🌐
15. The Menu widget
anzeljg.github.io › rin2 › book2 › 2405 › docs › tkinter › universal.html
26. Universal widget methods - Tkinter
If you do not pass a callback argument, ... function of the standard Python time module. ... Cancels a request for callback set up earlier .after(). The id argument is the result returned by the original .after() call. ... Requests that Tkinter call function func with arguments ...
🌐
Quora
quora.com › How-can-I-define-a-sequence-of-callback-functions-in-Python-Tkinter
How to define a sequence of callback functions in Python Tkinter - Quora
Answer: You can create your buttons inside a loop, just like any other time you want to do a series of operations that differ by some sequentially computable parameter(s). Button callback functions in TkInter have no parameters because they ...
🌐
GitHub
github.com › python › cpython › issues › 66410
Tkinter: Don't stringify callback arguments · Issue #66410 · python/cpython
August 17, 2014 - activity = <Date 2019-10-02.17:57:31.798> actor = 'serhiy.storchaka' assignee = 'serhiy.storchaka' closed = False closed_date = None closer = None components = ['Tkinter'] creation = <Date 2014-08-17.14:24:09.210> creator = 'serhiy.storchaka' dependencies = ['21585'] files = ['36393', '36406', '43468', '43476'] hgrepos = [] issue_num = 22214 keywords = ['patch'] message_count = 7.0 messages = ['225446', '225480', '225494', '268847', '268868', '268871', '268883'] nosy_count = 3.0 nosy_names = ['terry.reedy', 'gpolo', 'serhiy.storchaka'] pr_nums = ['16545'] priority = 'normal' resolution = None stage = 'patch review' status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue22214' versions = ['Python 3.7', 'Python 3.8', 'Python 3.9'] gh-66410: Do not stringify arguments of Tkinter callback #98592
Author   serhiy-storchaka
🌐
Python
bugs.python.org › issue22214
Issue 22214: Tkinter: Don't stringify callback arguments - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/66410
🌐
Python
mail.python.org › pipermail › tutor › 2017-April › 111036.html
[Tutor] Button command arguments: Using class to wrap function call versus using lambda in tkinter
April 23, 2017 - I came up with: ========================================================================== #!/usr/bin/env python3 import tkinter as tk # Command class to wrap function call with args # A.K.A. "currying" class Command: def __init__(self, callback, *args, **kwargs): self.callback = callback self.args = args self.kwargs = kwargs def __call__(self): return self.callback(*self.args, **self.kwargs) def callback(arg): print('You called callback with the following arg: %s' % arg) root = tk.Tk() tk.Button(root, text='Foo', command=Command(callback, 'Foo')).pack() tk.Button(root, text='Bar', command=Com
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 166751 › tkinter-button-widget-passing-parameter-to-function
python - tkinter button widget - passing parameter ... [SOLVED] | DaniWeb
January 7, 2009 - The Tkinter docs cover command callbacks and module naming in Python 3 (tkinter, not Tkinter) https://docs.python.org/3/library/tkinter.html. Widget references: Button(...).pack() returns None. If you need the button later, create it, then call pack() on a separate line. Here is an example how to use lambda to pass arguments in Tkinter's button command: import Tkinter as tk def getCond(par): firstCond = par #conditions.remove(firstCond) print firstCond #print conditions root = tk.Tk() firstCond = '' condNames = ['1', '2', '3', '4', '5', '6'] for item in condNames: …