In python you can use the following method to get the selected element in lisxtbox component.
A listbox shows a list of options. You can then click on any of those options. By default it won’t do anything, but you can link that to a callback function or link a button click.
Output:
Example 1: Tkinter listbox select event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# -*- coding: utf-8 -*- import tkinter as tk def getElement(event): selection = event.widget.curselection() index = selection[0] value = event.widget.get(index) result.set(value) print(index,' -> ',value) root = tk.Tk() root.title("Code4Example.com") # Add a title result =tk.StringVar() tk.Label(root,text="""Click Listbox Element""").grid(row = 0, column = 0) tk.Label(root,text="""result""", textvariable=result).grid(row = 1, column = 0) var2 = tk.StringVar() var2.set(('Apple','Banana','Pear', 'Peach')) lb = tk.Listbox(root, listvariable=var2) lb.grid(row = 0, column = 1) lb.bind('<<ListboxSelect>>', getElement) #Select click root.mainloop() |
This code is a simple Tkinter program that displays a list of items in a listbox widget and prints the selected item to the console when it is clicked. Here is a more detailed explanation of how it works:
The getElement
function is defined to handle the <<ListboxSelect>>
event, which is triggered when an item in the listbox is clicked. The function takes an event object as an argument, which contains information about the event, such as the widget that triggered it and the index of the selected item.
The function gets the index of the selected item using the curselection
method of the widget and then uses the get
method to get the value of the selected item. The value is then set as the text of a label widget using the set
method of a StringVar
instance, and the index and value of the selected item are printed to the console.
The listbox widget is created using the Listbox
class and is populated with a list of items using the listvariable
option, which is set to a StringVar
instance. The bind
method is used to associate the <<ListboxSelect>>
event with the getElement
function.
Finally, the mainloop
method is called to start the event loop, which listens for user input and redraws the GUI as necessary. This is what allows the listbox to function properly.
Example 2:Select listbox element by double click
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# -*- coding: utf-8 -*- import tkinter as tk def getElement(event): selection = event.widget.curselection() index = selection[0] value = event.widget.get(index) result.set(value) print(index,' -> ',value) root = tk.Tk() root.title("Code4Example.com") # Add a title result =tk.StringVar() tk.Label(root,text="""Click Listbox Element""").grid(row = 0, column = 0) tk.Label(root,text="""result""", textvariable=result).grid(row = 1, column = 0) var2 = tk.StringVar() var2.set(('Apple','Banana','Pear', 'Peach')) lb = tk.Listbox(root, listvariable=var2) lb.grid(row = 0, column = 1) lb.bind('<Double-1>', getElement) #double click root.mainloop() |
This code creates a Tkinter GUI with a Listbox and a Label. The Listbox contains a list of fruit names and the Label is used to display the result of selecting an item from the Listbox.
When the user double-clicks on an item in the Listbox, the “getElement” function is called. This function gets the index and value of the selected item and displays the value in the Label. It also prints the index and value to the console.
Finally, the mainloop function is called to start the GUI event loop, which allows the GUI to respond to user interactions.
Python Tkinter listbox get selected item
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
.... .... def getElement(event): selection = event.widget.curselection() index = selection[0] value = event.widget.get(index) print(index,' -> ',value) .... .... lb = tk.Listbox(root, listvariable=var2) lb.grid(row = 0, column = 1) lb.bind('<<ListboxSelect>>', getElement) #Select click |