Home >Backend Development >Python Tutorial >How to Pass Arguments to a Tkinter Button's Command Function?
Question:
Consider the following Tkinter button:
import Tkinter as Tk win = Tk.Toplevel() frame = Tk.Frame(master=win).grid(row=1, column=1) button = Tk.Button(master=frame, text='press', command=action)
How can you pass arguments to the action method when the button is pressed?
Explanation:
The provided code calls the action method immediately when the button is created, rendering the button ineffective.
Solution:
To pass arguments, you can use a lambda function:
button = Tk.Button(master=frame, text='press', command=lambda: action(someNumber))
The lambda function binds the argument to the command without requiring an explicit wrapper method or modifying the original action.
The above is the detailed content of How to Pass Arguments to a Tkinter Button's Command Function?. For more information, please follow other related articles on the PHP Chinese website!