Pass argument to Tkinter button command

To pass an argument to a tkinter button command in python, you can use a lambda function or the functools.partialĀ  method .Here’s how you can do it with both methods:

code :

import tkinter as tk

def button_command(arg):

print(f”Button clicked with argument : {arg}”})

root = tk.TK()

button = tk.Button(root, text=”Click me”,command=lambda: button_command(“Hello, world!”))

button.pack()

root.mainloop()

OUT PUT:

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top