1,105,427 Community Members

Toplevel windows for all matching entries, tkinter

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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()
Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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()
Question Self-Answered as of 2 Years Ago
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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?

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

searchfor is in the Interface class.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: