954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Fixing an Label ugly display on Tkinter

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

{['Email: [email]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!

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

ugh, pardon my 1am grammar in the title

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

', '.join(Interface.result)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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

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

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

How about to change

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

from tuple to

Interface.result=query.capitalize()+':'+' ,'.join(ab.book[query])
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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?

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

ABOOK.dat only contains a dictionary

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

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

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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

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

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

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

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.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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.

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

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)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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 .

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I haven't but I will now, thanks

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

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: