Advertisement
here2share

# Tk_list_search.py

May 23rd, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. # Tk_list_search.py
  2.  
  3. import Tkinter as Tk
  4.  
  5. master = Tk.Tk()
  6.  
  7. listbox = Tk.Listbox(master)
  8. listbox.pack()
  9.  
  10. # Insert few elements in listbox:
  11. for item in ["zero", "one", "two", "three", "four", "five", "six", "seven"]:
  12.     listbox.insert(Tk.END, item)
  13. # Return index of desired element to seek for
  14. def check_index(element):
  15.    try:
  16.        index = listbox.get(0, "end").index(element)
  17.        return index
  18.    except ValueError:
  19.        print'Item can not be found in the list!'
  20.        index = -1 # Or whatever value you want to assign to it by default
  21.        return index
  22.  
  23. print check_index('three') # Will print 3
  24.  
  25. print check_index(100) # This will print:
  26.                        # Item can not be found in the list!
  27.                        # -1
  28.  
  29. Tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement