In version 3.0 of my address book I'm utilizing tkinter. The only problem thus far I'm having is that I have a very ugly display on a child window that shows the search result for a query. I'm getting

{Shoiukamp0@gmail.com[/email], Phone: (111)555-2222, Address: 122 C---- Ct., Essex, VT']}

The issue is that I'm looking for the text without the curly braces and list brackets, I tried substringing it but then nothing is displayed. Below are the segments of code related to this, note that searchfor for is a class method, also not that I'm aware that modifying the class attributes is not the best way to do this, but it worked best for me to make the child's label(this strategy is not my issue at hand).

def searchfor(self):
        query= self.search.get().capitalize()
        if query in ab.book:
            Interface.result=query.capitalize(),':',ab.book[query]
        else:
            Interface.result='Query not found.'
            print(Interface.result)
        show()

class Display_contact(Frame):
    '''Show a contact'''
    def __init__(self,master):
        super(Display_contact, self).__init__(master)
        self.grid()
        self.create_widget()

    def create_widget(self):
        self.contact=Label(self,text=Interface.result)
        self.contact.grid()

How could I go about making Interface.result ab.book[query] without the {[]}?
Thanks for the advice!

Recommended Answers

All 15 Replies

ugh, pardon my 1am grammar in the title

', '.join(Interface.result)

duh, of course... I'm very disappointed in myself. Thanks pyTony.

oh, almost. I'm still getting the []

changed Label to: self.contact=Label(self,text=''.join(Interface.result))

just a reminder that it's originally this:

'Email: %s\n, Phone: %s\n, Address: %s' % (self.email.get(),self.phone.get(),self.adrs.get())

How about to change

Interface.result=query.capitalize(),':',ab.book[query]

from tuple to

Interface.result=query.capitalize()+':'+' ,'.join(ab.book[query])

thanks, I'll try that, but I can't at the moment because of a semi-related issue that, for unknown reasons just appeared.I'm getting this

Traceback (most recent call last):
  File "E:\AddressBook\abclass.py", line 103, in <module>
    adb=pickle.load(f)
EOFError

for this:

import pickle
f=open('ABOOK.dat','rb+')       
adb=pickle.load(f)
ab=AddressBook(adb)

idk what this is. Any idea why that would happen, especially when it was fine before?

ABOOK.dat only contains a dictionary

Did you check the size of file, maybe it have been truncated to 0 length (eg opened for writing and closed without writing).

I'm actually positive that happened size=0KB. rrrr. how do I fix this?

had it fixed for a moment. The answer for anybody with this problem in the future is to open it interactively, place something in it, and close it.

>>> import pickle
>>> pickle.dump({'Joshua':'Me'},f)
>>> f.close()

note that f was already assigned to open('filepath.extension','wb+')

but now, after truncating it to 0 by an error in the code again, this is not working...

You must save it again or restore from backup, I think. You could try to put reading the old pickle inside try.. except EOFError: # make empty dictionary block.

commented: Worked great +3

pyTony, that does work.

import pickle
try:
    f=open('ABOOK.dat','rb+')
    adb=pickle.load(f)
    ab=AddressBook(adb)
except EOFError:
    f=open('ABOOK.dat','wb+')
    empdict={}
    pickle.dump(empdict,f)
    f.close()
    f=open('ABOOK.dat','rb+')
    adb=pickle.load(f)
    ab=AddressBook(adb)

Thanks, and that'll be good for future reference for others.

I meant you let normal saving routine to do the saving

import pickle
try:
    f=open('ABOOK.dat','rb+')
    adb=pickle.load(f)
    ab=AddressBook(adb)
except EOFError:
    adb={}
    ab=AddressBook(adb)

I haven't but I will now, thanks

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.