我试图熟悉kivy并且通过这样做我认为我可以为我访问最多的一些网站制作一个小型新闻提要阅读器。 (他们都有一个rss表格)
我努力将自己的头包裹在kivy周围,这似乎是我生命中的新怪物。
我有以下代码。我想要做的是创建一个按钮列表(我可以做得很好),然后让该列表可滚动(在一些网站上有很多新闻源)。
我很难在列表中获得可滚动部分。现在它只是试图将所有按钮放在窗口的底部,这使得它看起来都被压扁和粗糙,我认为这与size_hint_y = none有关,但我知道文档提到你需要禁用gridlayout上的hint_y可以使scrollview工作(或者至少我是如何解释它的)。
代码:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
#Importing modules for data capture for website and reading.
from urllib import urlopen
from re import findall, MULTILINE, sub
#news list data
heading = []
date = []
id_ref = []
URL_list = 'my_sites.xml'
O_WL = urlopen(URL_list).read()
class Screen(GridLayout):
def __init__(self,**kwargs):
super(Screen,self).__init__(**kwargs)
self.cols = 1
self.spacing = 2
self.size_hint_y=None
for i in range(len(id_ref)):
btn_new = Button(text = heading[i],height=50, text_size=(300,None),font_size='15sp')
self.add_widget(btn_new)
root = ScrollView(size_hint=(None,None),size=(400,400))
root.add_widget(self)
class rss_class(App):
def build(self):
O_WL = urlopen(URL_list).read()
id_ex = findall('<id>(.*)</id>',O_WL)
for i in id_ex:
id_ref.extend([i])
head_ex = findall('<headline>(.*)</headline>',O_WL)
for i in head_ex:
heading.extend([i])
date_ex = findall('<modifiedDate>(.*)</modifiedDate>',O_WL)
for i in date_ex:
date.extend([i])
print id_ref
return Screen()
runTouchApp(rss_class())
提前感谢您的帮助和指导。
答案 0 :(得分:1)
好吧,所以经过很多撞墙后,我已经设法回答了我自己的问题,但这带来了许多未知数。哦,学习新事物的快乐。哈哈。
这是我的代码回答了我自己的问题。我希望将来可以帮助其他人。
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
#Importing modules for data capture for website and reading.
from urllib import urlopen
from re import findall, MULTILINE, sub
#news list data
heading = []
date = []
id_ref = []
URL_list = 'my_sites.xml'
O_WL = urlopen(URL_list).read()
class Main_app(App):
O_WL = urlopen(URL_list).read()
id_ex = findall('<id>(.*)</id>',O_WL)
for i in id_ex:
id_ref.extend([i])
head_ex = findall('<headline>(.*)</headline>',O_WL)
for i in head_ex:
heading.extend([i])
date_ex = findall('<modifiedDate>(.*)</modifiedDate>',O_WL)
for i in date_ex:
date.extend([i])
def build(self):
Buttonlayout = GridLayout(cols=1, spacing=2, size_hint_y=None)
Buttonlayout.bind(minimum_height=Buttonlayout.setter('height'))
for i in range(len(id_ref)):
btn = Button(text=heading[i], size_hint_y=None, height=80,text_size=(350,None),font_size='12sp')
Buttonlayout.add_widget(btn)
root = ScrollView()
root.add_widget(Buttonlayout)
return root
if __name__ == '__main__':
Main_app().run()