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

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

{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!

2
Contributors
15
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
16
Views
Question
Answered
pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 0

ugh, pardon my 1am grammar in the title

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

', '.join(Interface.result)

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

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

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
6,304 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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
Skill Endorsements: 0

ABOOK.dat only contains a dictionary

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

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
6,304 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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
Skill Endorsements: 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...

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

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
6,304 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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
Skill Endorsements: 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)
pyTony
pyMod
Moderator
6,304 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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
6,304 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

I haven't but I will now, thanks

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pyTony

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.1176 seconds using 2.7MB