tkinter - Is is possible to search a listbox and return an index location? -
im having trouble finding answer this. is possible search tk listbox exact entry or string , return location or index in list?
thank you.
you can use get()
method 1 or more items list.
in first step, use get(0, end)
list of items in list; in second step use finding index of item given list containing in python forwards index()
method:
import tkinter tk master = tk.tk() listbox = tk.listbox(master) listbox.pack() # insert few elements in listbox: item in ["zero", "one", "two", "three", "four", "five", "six", "seven"]: listbox.insert(tk.end, item) # return index of desired element seek def check_index(element): try: index = listbox.get(0, "end").index(element) return index except valueerror: print'item can not found in list!' index = -1 # or whatever value want assign default return index print check_index('three') # print 3 print check_index(100) # print: # item can not found in list! # -1 tk.mainloop()
Comments
Post a Comment