I'm working on learning GUI development, I'm attempting to utilize the GUI for a user to search the dictionary: my code so far is

from tkinter import *

class Interface(Frame):
    def __init__(self, master):
        super(Interface, self).__init__(master)
        self.grid()
        self.create_widget()
        

    def create_widget(self):
        self.pwrd= Label(self, text='Address Book')
        self.pwrd.grid(row=0, column=2)
        self.searchlbl= Label(self, text='Enter Search Query:')
        self.searchlbl.grid(row=2, column=0)
        self.search= Entry(self)
        self.search.grid(row=2,column=1,columnspan=1)
        self.subsearch= Button(self, text='Submit', command=self.searchfor)
        self.subsearch.grid(row=2,column=2)
        self.searchresult= Text(self, width=40, height=10, wrap=WORD)
        self.searchresult.grid(row=3,column=0, columnspan=2, rowspan=2)

    def searchfor(self):
        query=str(self.search)
        self.searchresult.delete(0.0,END)
        if query in ab.book:
            result=ab.book[str(query)]
            self.searchresult.insert(0.0,result)
        else:
            result='Query not found.'
            self.searchresult.insert(0.0,result)

class AddressBook(object):
    """AddressBook Features"""
    def __init__(self, book):
        self.book=book

    def search(self,query):
        query=query.lower()
        if query in self.book:
            return self.book[query]



if __name__=='__main__':
    ab={'josh':'me'}
    ab=AddressBook(ab)
    root=Tk()
    root.title('Address Book')
    root.geometry('1500x800')
    Interface(root)
    root.mainloop()

regardless of the user input the Text displays 'Query not found', upon further examination I found that self.book became {{'josh':'me'}} why did it double the dictionary and is that why the search is being messed up?

Recommended Answers

All 3 Replies

I would suggest that you print "ab" prior to the search. Since it is not passed to the class, the class has no idea what it is.

if __name__=='__main__':
    ab={'josh':'me'}
    AB=AddressBook(ab)  ## AB=class instance, ab=dict

class Interface(Frame):
    def __init__(self, master, AB_instance):
        self.AB_instance=AB_instance

        if query==self.AB_instance.book:

Interface(root, AB) 
#
##--------- or -------------------------------------------------------
if __name__=='__main__':
    ab={'josh':'me'}
#    AB=AddressBook(ab)  ## AB=class instance, ab=dict

class Interface(Frame):
    def __init__(self, master, ab_dict):
        self.ab_dict=ab_dict

        if query in self.ab_dict:  ## search for "josh"

Interface(root, ab)

still having the issue. I printed the query so that it would show up in the IDLE shell and the query 'josh' = .46072912.46072880

I figured it out. query needs to = self.search.get()

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.