- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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
Running multiple commands when a button is pressed in Tkinter
The Button widget provides a way to communicate through all the existing functionalities of an application. We can perform a certain action with the help of a Button that encapsulates the function and the objects. However, there might be cases when we want to perform multiple operations with a single button. This can be achieved by defining the lambda functions which target multiple events or callback in the application.
In this example, we will add multiple events to a specific Button.
#Import the Tkinter Library from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry of window win.geometry("700x350") #Define functions def display_msg(): label.config(text="Top List of Programming Language") def show_list(): listbox= Listbox(win, height=10, width= 15, bg= 'grey', activestyle= 'dotbox',font='aerial') listbox.insert(1,"Go") listbox.insert(1,"Java") listbox.insert(1,"Python") listbox.insert(1,"C++") listbox.insert(1,"Ruby") listbox.pack() button.destroy() #Create a Label widget to display the message label= Label(win, text= "", font= ('aerial 18 bold')) label.pack(pady= 20) #Define a Button widget button= Button(win, text= "Click Here",command= lambda:[display_msg(), show_list()]) button.pack() win.mainloop()
Running the above code will display a window containing a button.
When we click the Button, it will perform two tasks in parallel. It will display a window containing a Label widget and a List of strings.
- Related Articles
- Determine which Button was pressed in Tkinter
- Tkinter button commands with lambda in Python
- How to clear the Entry widget after a button is pressed in Tkinter?
- Running Multiple Commands in the Background on Linux
- Execute a script when a mouse button is pressed down on an element in HTML?
- How can I identify when a Button is released in Tkinter?
- How can I play a sound when a Tkinter button is pushed?
- How do I make a pop-up in Tkinter when a button is clicked?
- Add a pressed effect on button click with CSS
- Add a blue background color to simulate a pressed button in Bootstrap
- Call the same function when clicking a Button and pressing Enter in Tkinter
- Creating a button in tkinter in Python
- Creating a Browse Button with Tkinter
- How to Disable / Enable a Button in TKinter?
- How to update a Button widget in Tkinter?