如何通过ScreenManager在kv语言上使用Kivy的RecycleView?

时间:2018-09-11 00:37:10

标签: python kivy

我在Google的Firebase上有一个数据库,可以很好地工作,我可以轻松地将数据保存在那里。我想为我的应用返回此数据,但是在遇到问题之前,我无法在Kivy上列出任何内容。

我想使用Kivy的ListView,但是在文档中建议使用RecycleView。但是我看不懂文档。我有些怀疑。

如果您可以阅读RecycleView的文档,您将看到以下示例:

Builder.load_string('''
<RV>:
    viewclass: 'Label'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
''')

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]


class TestApp(App):
    def build(self):
        return RV()

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

但是我使用ScreenManager来控制屏幕,然后在TestApp类中返回“ sm”,如文档示例所示:

# Declare both screens
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class TestApp(App):
    def build(self):
        return sm

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

如果您看到语法不同,那么在这里我不知道该如何编码。我想继续使用ScreenManager来控制屏幕,并使用RecycleView返回列表中的数据。

如何将RecycleView与ScreenManager一起使用?这是我的main.py,在另一个文档中配置屏幕,我也使用ki语言。因此,如果大家能为我做榜样,我将不胜感激。

import kivy
from kivy.app import App, Builder
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager
from telas.telas import Acesso, Comprando, Vendendo, CadastrarEvento


kivy.require('1.10.1')
Builder.load_file('ing.kv')
Config.read('config.ini')
sm = ScreenManager()

sm.add_widget(Acesso(name='acesso'))
sm.add_widget(Comprando(name='comprando'))
sm.add_widget(Vendendo(name='vendendo'))
sm.add_widget(CadastrarEvento(name='cadastrarEvento'))
sm.add_widget(ListaEventos(name='listaEventos'))

class IngApp(App):
    def build(self):
        return sm

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

这是我第一次尝试的kv

<ListaEventos>:
    canvas:
        Rectangle:
            source: 'design/fundo.png'
            size: self.width, self.height    


    viewclass: 'Label'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

ListaEventos:

class ListaEvento(Screen, RecycleView):
    def __init__(self, **kwargs):
        super(ListaEvento, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(20)] 

1 个答案:

答案 0 :(得分:0)

您不应该继承2个小部件,但是将绘制什么小部件?例如,如果要让图像表现得像按钮,则必须从Image小部件和ButtonBehavior类继承,即,从视觉上看,它是图像,但添加了按钮行为。

因此,要解决您的问题,使用继承不是正确的方法,而是使用构成,即将RecyclerView添加为Screen的儿子。

*。py

import kivy
from kivy.app import App, Builder
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen

class ListaEventos(Screen):
    def __init__(self, **kwargs):
        super(ListaEventos, self).__init__(**kwargs)
        # assigning data in RecyclerView
        self.rv.data = [{'text': str(x)} for x in range(100)]


kivy.require('1.10.1')
Builder.load_file('ing.kv')
Config.read('config.ini')

sm = ScreenManager()
sm.add_widget(ListaEventos(name='listaEventos'))


class IngApp(App):
    def build(self):
        return sm


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

ing.kv

<ListaEventos>:
    rv: rv # expose the widget

    canvas:
        Rectangle:
            source: 'design/fundo.png'
            size: self.width, self.height

    RecycleView:
        id: rv
        viewclass: 'Label'
        RecycleBoxLayout:
            default_size: None, dp(56)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'