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
#2
If you ran the code below what do you think it would print? If the if statement was to changed to "if i > 6:" what would it print? Answer that, without actually running the code, and you will know not only why only the first matching item in the itemslist is selected but also why the message box is not drawn.

for i in range(5):
    if i > 2:   # Change this to 6, what is printed then?
        break
    print(i)
else:
    print('Counted to end')
What is printed? If I change the if comparison to i < 12, what gets printed then?
Reply
#3
First try (original code)
0
1
2

After changing to 6

0
1
2
3
4
Counted to end

Had to run the code
Reply
#4
Does that mean you understand how the break and for...else commands work? If you want to write python programs you need to understand a few basic concepts. You need to know about dictionaries, lists and tuples. You need to understand functions, arguments and return values. You need to know about for (and else can be useful), if-elif-else, break and continue. If you really understand that stuff it is amazing how much easier it is to pick up all the other stuff because the same ideas keep getting used over and over.
Reply
#5
YES I DO UNDERSTAND!

Oops! CAPS LOCK was on!
Reply
#6
So did that help you solve your problem? Can you see how the break in your code is preventing other code inside your loop from running. Code you probably want to run. Do you now see that you probably wanted to use continue instead of break, but that using for..break..else provides a cleaner solution?

DO YOU!
Oops! CAPS LOCK was on!
Reply
#7
Break stops a loop. It is breaking the loop after it finds one which is why it doesn't find them all. So when do I stop the loop so that it finds everything?
Reply
#8
Read your question out loud to yourself. The answer is there.

I think your problem is you are worrying too much about how to do stuff in python and it is preventing you from seeing how simple this is. I will rephrase your question.

I want to pick up all the rocks. How will I know when to stop picking up rocks?

I use rocks not because I think your head is full of rocks, but because most programming problems are so simple a cavman could do it (reference to old GEICO commercial in the USA). If programming was really hard there wouldn't be millions of people who can program. But new programmers often make the mistake of trying to design code at the keyboard. At the keyboard you need to think about syntax and spelling and punctuation and what commands are available and what arguments the commands take and..... When you become proficient in a language most of that stuff happens subconsciously, but when you are starting it can be overwhelming and make really smart people stupid.

Right now I have two pads of paper and pens and pencils sitting next to me. When I have a problem with the program I am working on it is the pencil and paper I reach for first, not the keyboard. I write my solution using pictures and natural language, not Python or C. When I am happy with the solution I translate it into programming language. Sometimes the translation requires modifications to the solution. but usually not.

Your problem is: If I want to process all the items in a list, how do I know when to stop?

See? Not any harder than picking up rocks.
Reply
#9
Thanks. Wise advice!
Reply
#10
Got it to work!
Reply


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,766 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