kivy:我的screenmanager代码无法使用scrollview

时间:2015-11-24 05:25:02

标签: android uiscrollview kivy

尝试让我的kivy使用screenmanager和scrollview。我不明白为什么它不起作用。如果我删除scrollview并将其保存为一个简单的框和按钮(如代码中的屏幕2),它可以正常工作。只要我尝试使用按钮添加滚动视图,我得到的就是一个空白屏幕。

有人可以帮助指出我的错误。我是kivy的新手。

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView

from urllib import urlopen
from re import findall, MULTILINE, sub

heading = []
date = []
id_ref = []

URL_list = 'web_site_here.xml'

O_WL = urlopen(URL_list).read()


class ScreenOne(Screen):

    def __init__ (self,**kwargs):
        super (ScreenOne, self).__init__(**kwargs)
        Buttonlayout = GridLayout(orientation='vertical',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')
            btn.bind(on_press=(lambda a:self.changer()))
            Buttonlayout.add_widget(btn)
        root = ScrollView()
        root.add_widget(Buttonlayout)

    def changer(self,*args):
        self.manager.current = 'story_screen'

class ScreenTwo(Screen):

    def __init__(self,**kwargs):
        super (ScreenTwo,self).__init__(**kwargs)
        story_box = BoxLayout(orientation='vertical')
        story_heading = Label(text="testing")
        back = Button(text="Back",size_hint_y=None, size_y=50)
        back.bind(on_press=self.changer)
        story_box.add_widget(story_heading)
        story_box.add_widget(back)
        self.add_widget(story_box)

    def changer(self,*args):
        self.manager.current = 'but_screen'

class TestApp(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):
        my_screenmanager = ScreenManager()
        screen1 = ScreenOne(name='but_screen')
        screen2 = ScreenTwo(name='story_screen')
        my_screenmanager.add_widget(but_screen)
        my_screenmanager.add_widget(story_screen)
        return my_screenmanager

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

您至少需要在root

中添加ScreenOne
class ScreenOne(Screen):

    def __init__ (self,**kwargs):
        ...
        root = ScrollView()
        root.add_widget(Buttonlayout)
        self.add_widget(root)