Tkinter ListBox check if the specified item is selected
You can use the selection_includes() method to check if the index item specified in the ListBox is selected, if the specified index item is selected it will return True, otherwise it will return False.
Tkinter ListBox check if the specified item is selected Example#1
Check if the item of index 3 is selected, if it is selected click the Check button to display True, otherwise display False.
from tkinter import *
def callback(): # Print the selected item
print(lb.selection_includes(3)) # Print the selected item
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x250")
lb = Listbox(root,selectmode=MULTIPLE)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=5)
btn = Button(root,text="Check",command=callback)
btn.pack(pady=5)
root.mainloop()
Output: