Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the index of an item in tkinter.Listbox?

For example:

import tkinter as tk
x = tk.Tk()
y = tk.Listbox(x, bg="white")
y.insert(tk.END, "value1")
y.insert(tk.END, "value2")
>>>return y.index("value1")

This should output 0 but it just gives me an error, I did some reasearch and I can't find anything that has so I ask here.

like image 749
jb3 Hd Avatar asked Oct 15 '15 09:10

jb3 Hd


1 Answers

The index method of the listbox isn't the same as the index method of a python list. For the listbox, it translates something like "end" or "@x,y" into a numerical index.

To search, get the values as a python list, and then use the index method of the list:

index = y.get(0, "end").index("value1")
like image 112
Bryan Oakley Avatar answered Oct 13 '22 00:10

Bryan Oakley