We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,870 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Toplevel windows for all matching entries, tkinter

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?

2
Contributors
7
Replies
18 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 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.

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 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()
pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 0

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

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 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()
pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 0
Question Self-Answered as of 1 Year Ago

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?

pyTony
pyMod
Moderator
6,301 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

searchfor is in the Interface class.

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 0

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

pyTony
pyMod
Moderator
6,301 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0754 seconds using 2.67MB