Python 如何在ScreenManager中使用Kivy on kv语言的RecycleView?
我在谷歌的Firebase上有一个数据库运行良好,我可以轻松地将数据保存在那里。我想为我的应用程序返回这些数据,但在我遇到问题之前,我无法在Kivy上列出任何内容 我想使用Kivy的ListView,但在文档中建议使用RecycleView。但是我不能理解文档。我有些怀疑 如果您可以阅读的文档,您将看到以下示例:Python 如何在ScreenManager中使用Kivy on kv语言的RecycleView?,python,kivy,Python,Kivy,我在谷歌的Firebase上有一个数据库运行良好,我可以轻松地将数据保存在那里。我想为我的应用程序返回这些数据,但在我遇到问题之前,我无法在Kivy上列出任何内容 我想使用Kivy的ListView,但在文档中建议使用RecycleView。但是我不能理解文档。我有些怀疑 如果您可以阅读的文档,您将看到以下示例: Builder.load_string(''' <RV>: viewclass: 'Label' RecycleBoxLayout: def
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来控制屏幕,并使用RecycleView在列表中返回我的数据
如何与我的ScreenManager一起使用RecycleView?这是我的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'
您不应该继承2个小部件,而是应该绘制哪个小部件?例如,如果希望图像的行为类似于按钮,则必须从图像小部件和ButtonBehavior类继承,也就是说,视觉上它是一个图像,但添加了按钮行为 因此,要解决您的问题,使用继承而使用组合是不正确的,也就是说,将RecyclerView添加为屏幕的子元素 *.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'
:
rv:rv#公开小部件
画布:
矩形:
资料来源:“design/fundo.png”
尺寸:自宽、自高
回收审查:
id:rv
viewclass:“标签”
循环利用布局:
默认大小:无,dp(56)
默认大小提示:1,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
显示你的.kv…好吧,我没有为这件事设置kv,你为什么不尝试一下?那个屏幕会是回收视图吗?该RecyclerView中将显示哪些数据?示例中是否会使用相同的数据?但我尝试了。我也不能使用这个例子,所以我想从这个开始(文档的例子)。将来,我想显示我的Firebase的数据,比如一个或两个字段,带有名称和日期,例如,我认为RecyclerViews是一个小部件,就像按钮一样,我可以在我的屏幕上使用它。非常感谢!!你帮了我很多。对错误表示抱歉,再次感谢您的关注。这正是我想要的,现在我可以做其他事情了谢谢你。这真的很有帮助。
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()
<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'