I've been working on getting tkinter to open a window for all dictionary items with keys matching a query. Below is a VERY RAW piece of code to show what I'm talking about. show() makes the toplevel windows. Also, not that it has so much going on because I isolate small sections of code to work on in seperate windows then copy and paste them into where they would go, then test and leave other things as comments so if something goes wrong I can temporarily revert back.

def searchfor(self):
        count=0
        list1=[]
        query=self.search.get().capitalize()
        for key in ab.book.keys():
            if query[0:2]==key[0:2]:
                count+=1
                print(count)
                list1.append(key)
                for items in list1:
                    Interface.result=query.capitalize(),str(ab.book[key])
                    show()
                    list1=[]          
##        for key in ab.book.keys():
            if query[0:2]==key[0:2]:
                query=key
                Interface.result=query.capitalize(),str(ab.book[query])
                show()
                break
            else:
                Interface.result='Query not found.'
                print(Interface.result)
        for key in urlinfo.keys():
            if query[0:2]==key[0:2]:
                query=key
                Interface.url=str(urlinfo[query])
                show()
                break

this causes there to be a duplicate window for one of them. Say I enter Josh and I have 2 Josh's, they both pop up, but one will have an extra window. Ideas?

print(count) was just for me to see if other things were working correctly. Also, once Interface.result and Interface.url are set by the first then it stay the same for the second.

something like this seems to be working. It was smarter to use a dictionary. Don't know why I didn't think of it before, I guess sometimes you can get stuck in one way of thinking about a solution. Sometimes, especially in programming it seems, one must think outside of their own self-defined box. It looks MUCH better too.

def searchfor(self):
        query=self.search.get().capitalize()
        self.search.delete(0,END)
        self.dict1={}
        for key in ab.book.keys():
            if query[0:2]==key[0:2]:
                query=key
                dict2={query:ab.book[query]}
                self.dict1.update(dict2)
            else:
                Interface.result='Query not found.'
        if len(self.dict1.keys())>1:
            for key in self.dict1.keys():
                Interface.result=key.title,str(ab.book[key])
                show()
        else:
            show()

however, as is this only works if there is more than one contact by the same name. Ohhh darn...

and the resolution to that issue. Sorry to keep everybody so updated,

def searchfor(self):
        query=self.search.get().capitalize()
        self.search.delete(0,END)
        self.dict1={}
        for key in ab.book.keys():
            if query[0:2]==key[0:2]:
                query=key
                dict2={query:ab.book[query]}
                self.dict1.update(dict2)

        if len(self.dict1.keys())>1:
            for key in self.dict1.keys():
                Interface.result=key.title(),str(ab.book[key])
                for urlkey in urlinfo.keys():
                    if key==urlkey:
                        Interface.url=str(urlinfo[key])
                show()
        else:
            for key in ab.book.keys():
                if query[0:2]==key[0:2]:
                    query=key
                    Interface.result=query.title(),str(ab.book[query])
                    for urlkey in urlinfo.keys():
                        if query==urlkey:
                            Interface.url=str(urlinfo[query])
                    break
                else:
                    print('lies')
                    Interface.result='Query not found.'
            show()

Check it out, what is ab? It is one global instance of Addressbook? Which class searchfor belongs, isn't it Adressbook class? Shouldn't you refer the class instance as self (again), not ab?

searchfor is in the Interface class.

Then Interface result shoud be self.result and probably ab should be self.ab.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.