Reputation: 81
I'm inserting some data into listbox and when I try to read that placed in index, the program is returning a consecutive numbers like 0,1,2,3 as index and not the id used in lb.insert.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('300x200')
lb = tk.Listbox(root)
lb.insert(5,'data1')
lb.insert(8,'data2')
lb.insert(13,'data3')
lb.insert(55,'data4')
lb.insert(21,'data5')
but = ttk.Button(root,text='command',command=lambda:ReadIndex())
lb.pack()
but.pack()
def ReadIndex():
index = lb.index(lb.curselection())
print(index)
How can i get the index that wrote in .insert?
Upvotes: 1
Views: 2108
Reputation: 385890
How can i get the index that wrote in .insert?
You can't.
The "id" that you think you're giving isn't an id, it's an index. And if the index is larger than the last index, it's ignored and the item is placed at the end.
In other words, when you do this:
lb.insert(5,'data1')
lb.insert(8,'data2')
Then 'data1
will be inserted at index 0 (zero), and 'data2'
will be inserted at index 1 (one). There is no way to retrieve the original values 5 and 8.
Upvotes: 3