简体   繁体   English

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

[英]How can I use the RecycleView of Kivy on kv language with ScreenManager?

I have a database on Firebase of Google working well, I can save my data there easily.我在 Google 的 Firebase 上有一个运行良好的数据库,我可以轻松地将数据保存在那里。 I would like to return this data for my app, but before I have problems with this, I can't list anything on Kivy.我想为我的应用返回这些数据,但在我遇到问题之前,我无法在 Kivy 上列出任何内容。

I would want to use the ListView of Kivy, but in the documentation is recommended to use the RecycleView.我想使用 Kivy 的 ListView,但在文档中建议使用 RecycleView。 But I can't understand the documentation.但我无法理解文档。 I have some doubts.我有些疑惑。

If you can read the docs of RecycleView , you'll see this as an example:如果您可以阅读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()

But I'm using the ScreenManager to control my screens, then, in the TestApp class I return 'sm', like this example of the documentation:但是我使用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()

If you see the syntaxes are different, and it's here where I don't know how to code this.如果您看到语法不同,那么我不知道如何对此进行编码。 I would like to keep using the ScreenManager to control the screens and use a RecycleView to return my data in a list.我想继续使用 ScreenManager 来控制屏幕并使用 RecycleView 在列表中返回我的数据。

How can I use the RecycleView with my ScreenManager?如何将 RecycleView 与我的 ScreenManager 一起使用? This is my main.py, I configure the screen in another document, and I use the ki language too.这是我的main.py,我在另一个文档中配置了屏幕,我也是用ki语言的。 So if you all can to do an example to me I will be grateful.所以如果你们都可以给我做一个例子,我将不胜感激。

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()

Here the kv that I tried the first time这是我第一次尝试的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)] 

You should not inherit from 2 widgets but what widget is going to be painted?您不应该继承 2 个小部件,而是要绘制哪个小部件? For example, if you want an image that behaves like a button, you must inherit from the Image widget and the ButtonBehavior class, that is, visually it is an image but added the button behavior.例如,如果您想要一个行为像按钮的图像,则必须从 Image 小部件和 ButtonBehavior 类继承,也就是说,从视觉上看,它是一个图像,但添加了按钮行为。

So to solve your problem it is not correct to use the inheritance but the composition, that is, to add the RecyclerView as a son of the Screen.所以要解决你的问题,使用继承而不是组合是不正确的,即将RecyclerView添加为Screen的儿子。

*.py *.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 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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