简体   繁体   中英

kivy: My screenmanager code is not working with scrollview

Trying to get my kivy to work with screenmanager and scrollview. I cannot see why it is not working. If I remove the scrollview and just keep it as a simple box and button (like screen 2 in code) it works fine. As soon as I try to add the scroll view with buttons all I get is a blank screen.

Can somebody help point out my mistake. I am new to 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()

Thanks in advance.

You need, at least, to add your root in your ScreenOne

class ScreenOne(Screen):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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