- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Tkinter button commands with lambda in Python
Lamda Functions (also referred to as Anonymous Function in Python) are very useful in building Tkinter GUI applications. They allow us to send multiple data through the callback function. Lambda can be inside any function that works as an anonymous function for expressions. In Button Command, lambda is used to pass the data to a callback function.
Example
In this example, we will create an application that will have some buttons in it. 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 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()
Output
Running the above code will display a window that contains some buttons. Each button passing a text message as the argument to a common callback function using lambda function.
Now, click each Button to display the message on the screen.
- Related Articles
- Running multiple commands when a button is pressed in Tkinter
- Creating a button in tkinter in Python
- Add style to Python tkinter button
- Creating a Browse Button with Tkinter
- Change command Method for Tkinter Button in Python
- Add image on a Python Tkinter button
- How do I change button size in Python Tkinter?
- Setting the position on a button in Tkinter Python?
- How to exit from Python using a Tkinter Button?
- How to stop a loop with a stop button in Tkinter?
- Call a Function with a Button or a Key in Tkinter
- On/Off Toggle Button Switch in Tkinter
- Determine which Button was pressed in Tkinter
- How to reset the background color of a Python Tkinter button?
- Python Pandas CategoricalIndex - Rename categories with lambda
