gpt4 book ai didi

python - Kivy 标签仅显示文本循环的最后一个元素

转载 作者:太空宇宙 更新时间:2023-11-03 21:45:33 25 4
gpt4 key购买 nike

嗨,我正在使用 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 应用程序仅显示循环中的最后一个标题

即:皇家马德里同意切尔西足球俱乐部签下门将蒂博·库尔图瓦

关于我缺少什么有什么想法吗?任何帮助将不胜感激。

最佳答案

问题很简单,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:

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

关于python - Kivy 标签仅显示文本循环的最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52523636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com