Ярлык Kivy показывает только последний элемент текстового цикла - PullRequest
0 голосов
/ 26 сентября 2018

Привет! Я использую Feedparser для получения новостей rss и отображения заголовков новостей

import feedparser
import kivy
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):

 def build(self):
    d=feedparser.parse('https://en.wikinews.org/w/index.php?title=Special:NewsFeed&feed=rss&categories=Published&notcategories=No%20publish%7CArchived%7cAutoArchived%7cdisputed&namespace=0&count=15&ordermethod=categoryadd&stablepages=only')
    print (len(d['entries']))
    for post in d['entries']:
        news=post.title
        print(news)
    return Label(text=news)


if __name__ == '__main__':
    MyApp().run()

т.е. news=post.title с использованием метки kivy.

Первоначально вывод был следующим:

Study suggests Mars hosted life-sustaining habitat for millions of years
NASA's TESS spacecraft reports its first exoplanet
Russians protest against pension reform
US rapper Mac Miller dies at home in Los Angeles
Creativity celebrated at Fan Expo Canada 2018 in Toronto
Brisbane, Australia Magistrates Court charges two cotton farmers with $20m fraud  
Fossil genome shows hybrid of two extinct species of human
Singer Aretha Franklin, 'queen of soul', dies aged 76
New South Wales, Australia government says entire state in winter 2018 drought
Real Madrid agrees with Chelsea FC to sign goalkeeper Thibaut Courtois

Но, всякий раз, когда я запускаю программу, приложение Kivy показывает только последний заголовок в цикле

т.е.: Real Madrid agrees with Chelsea FC to sign goalkeeper Thibaut Courtois

Есть идеи о том, что мне не хватает?любая помощь будет благодарна.

1 Ответ

0 голосов
/ 26 сентября 2018

Проблема проста, news перезаписывается в цикле, поэтому принимает последнее значение, возможное решение состоит в объединении текстов:

import feedparser
import kivy
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):
    def build(self):
        url = 'https://en.wikinews.org/w/index.php?title=Special:NewsFeed&feed=rss&categories=Published&notcategories=No%20publish%7CArchived%7cAutoArchived%7cdisputed&namespace=0&count=15&ordermethod=categoryadd&stablepages=only'
        d=feedparser.parse(url)
        news = ""
        for post in d['entries']:
            news += post.title + "\n"
        return Label(text=news)

if __name__ == '__main__':
    MyApp().run()

enter image description here

Другой вариант - использовать BoxLayout и создать несколько Label s:

import feedparser
import kivy
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout


class MyApp(App):
    def build(self):
        root = BoxLayout(orientation='vertical')
        url = 'https://en.wikinews.org/w/index.php?title=Special:NewsFeed&feed=rss&categories=Published&notcategories=No%20publish%7CArchived%7cAutoArchived%7cdisputed&namespace=0&count=15&ordermethod=categoryadd&stablepages=only'
        d=feedparser.parse(url)
        for post in d['entries']:
            label = Label(text=post.title)
            root.add_widget(label)
        return root

if __name__ == '__main__':
    MyApp().run()

enter image description here

...