Python Forum
Listbox search code partially works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listbox search code partially works
#1
Hi everyone on the Python Forum!

I have some search code that partially works thanks to a helpful forum post from deanhystad!
I still have a few glitches.

class SearchWin:
    def searchList(self):
        content = self.searchTerms.get()
        next_index = 0
        elements = int(itemslist.size())
        for i in range(0, elements):
            if content in itemslist.get(next_index):
                itemslist.select_set(next_index)
                break
            else:
                if next_index < elements - 1:
                    next_index += 1
                    continue
                else:
                    messagebox.showerror("Item not found", "No item could be found containing " + "'" + content + "'")
             
    def __init__(self):
        searchPrompt = Tk()
        searchPrompt.title("Search list")
        #searchPrompt.iconbitmap("feather.ico")
        searchPrompt.resizable(0, 0)
 
        searchText = Label(searchPrompt, text = "Search terms")
        searchText.pack()
 
        Label(searchPrompt).pack()
 
        self.searchTerms = Entry(searchPrompt, width = 30)
        self.searchTerms.pack(padx = 10)
 
        Label(searchPrompt).pack()
 
        searchbtn = Button(searchPrompt, text = "Search", command = self.searchList)
        searchbtn.pack()
Glitches:

1. Messagebox not displayed (line 15) when item not found
2. Not all items that contain the search term are highlighted (just the first one it finds)
Reply


Messages In This Thread
Listbox search code partially works - by chesschaser - Mar-26-2020, 10:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,527 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  [Tkinter] add search bar = search for input in all computer directory francisco_neves2020 15 10,763 Apr-14-2019, 07:29 PM
Last Post: francisco_neves2020
  Code fails on Mac, works on Windows and Raspberry Pi eugenedakin 4 3,964 May-30-2018, 03:49 AM
Last Post: eugenedakin
  [Tkinter] THow to search a string in a file and display it in listbox by Tkinter mosavit 2 5,605 Jun-21-2017, 08:02 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020