- 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
How to capture events on Tkinter child widgets?
Suppose we are creating an application that interacts with the user clicks on the Button visible in the application. To understand how exactly events work, we have to create a callback function as well as a trigger that will execute an event. Whenever the user clicks the button, the event happens and it needs to be captured on the screen.
In this example, we will create a Listbox widget that will have a list of items in it. When we select an item, then it will capture what the user has clicked. To figure out the captured event, we can use the print() function to print on the screen.
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Create a Listbox widget lb=Listbox(win) lb.pack(expand=True, fill=BOTH) # Define a function to edit the listbox ite def save(): for item in lb.curselection(): print("You have selected "+ str(item+1)) # Add items in the Listbox lb.insert("end","item1","item2","item3","item4","item5") # Add a Button To Edit and Delete the Listbox Item ttk.Button(win, text="Save", command=save).pack() win.mainloop()
Executing the above code will display a window with a list of items in it. If we click the "Save" button, it will tell us what event has been captured.
Now, select and item from the list and click the "Save" button. It will print on the console the item you have selected.
You have selected 3
- Related Articles
- How to create transparent widgets using Tkinter?
- How to delete Tkinter widgets from a window?
- How to show and hide widgets in Tkinter?
- How to capture virtual keyboard show/hide events in Android ?
- How to set the border color of certain Tkinter widgets?
- Showing and Hiding widgets in Tkinter?
- How to bind events to Tkinter Canvas items?
- How do you overlap widgets/frames in Python tkinter?
- Configure tkinter/ttk widgets with transparent backgrounds
- How to position Tkinter widgets so that they are not stuck together?
- TkInter keypress, keyrelease events
- Adding a scrollbar to a group of widgets in Tkinter
- How to add space between two widgets placed in a grid in tkinter?
- How to bind multiple events with one "bind" in Tkinter?
- How to set padding of all widgets inside a window or frame in Tkinter?