简体   繁体   中英

For statement in label Python Tkinter

I want to display all results from the dictionary from Facebook to label in Python with Tkinter library. Label displays only first or last record. I'm using for loop. This is my code:

def loopInLabel(self, newsfeedData):
    for item in newsfeedData:
        try:
            a = ("Name: " + item['name'] + '\n' + "Message: " + item['message'] + '\n' + "Description: " + item['description'] + '\n')
            return a
        except KeyError:
            pass

And dispaly in label

def facebookEvent(self, label):
    newsfeed = F.get('/me/home', {'fields':'name,description,message'})
    newsfeedData = newsfeed["data"]
    label.config(text=self.loopInLabel(newsfeedData))

All is ok when I print it to the console with normal print()

If you're trying to display all of your news feed items at once in a single label, don't return inside the loop. Append the items to a list and return the whole thing when you're done.

def loopInLabel(self, newsfeedData):
    lines = []
    for item in newsfeedData:
        try:
            a = ("Name: " + item['name'] + '\n' + "Message: " + item['message'] + '\n' + "Description: " + item['description'] + '\n')
            lines.append(a)
        except KeyError:
            pass
    return " | ".join(lines)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM