Tkinter ListBox Pass back the specified index item
If the get() method contains a parameter, it means that the element content of this index item is passed back.
Tkinter ListBox Pass back the specified index item Example#1
After creating the Listbox, pass back the item with index 1.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
print(lb.get(1))
root.mainloop()
Output:
If there are two parameters within the get() method, it means that the interval option is passed back, the first parameter is the starting index item of the interval, the second parameter is the ending index item of the interval, and the value passed back is passed back in a tuple.
Tkinter ListBox Pass back the specified index item Example#2
After creating the Listbox, pass back the items with indexes 1 to 3.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
print(lb.get(1,3))
root.mainloop()
Output: