1,105,208 Community Members

Fixing an Label ugly display on Tkinter

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

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!

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

ugh, pardon my 1am grammar in the title

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
 
1
 

', '.join(Interface.result)

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

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

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

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())
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
 

How about to change

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

from tuple to

Interface.result=query.capitalize()+':'+' ,'.join(ab.book[query])
Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

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

ABOOK.dat only contains a dictionary

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
 

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

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

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

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

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...

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
 
1
 

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.

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

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.

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
 

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)
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
 

Have you by the way followed advice in by signature linked post and looked code in code snippets section? i have posted quite a few myself like http://www.daniweb.com/software-development/python/code/283859.

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

I haven't but I will now, thanks

Question Answered as of 2 Years Ago by pyTony
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: