简体   繁体   English

对于标签Python Tkinter中的声明

[英]For statement in label Python Tkinter

I want to display all results from the dictionary from Facebook to label in Python with Tkinter library. 我想显示来自Facebook字典的所有结果,并使用Tkinter库在Python中添加标签。 Label displays only first or last record. 标签仅显示第一条或最后一条记录。 I'm using for loop. 我正在使用for循环。 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() 当我使用正常的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. 如果您试图一次在单个标签中显示所有新闻源项目,请不要在循环内return 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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