[英]Can I add widgets in a Recycleview from the kv in kivy
I'm trying to add Buttons, Labels and Image in a recycleview.But the Screen stayed in black when running the code.我正在尝试在回收视图中添加按钮、标签和图像。但是在运行代码时屏幕保持黑色。 I can't see my Buttons.我看不到我的按钮。 I want this work from my kv.file.. I mean I want to add buttons, labels and images from my kv.file to a Recycleview (Boxlayout or Gridlayout)我想从我的 kv.file 中进行这项工作。我的意思是我想将我的 kv.file 中的按钮、标签和图像添加到 Recycleview(Boxlayout 或 Gridlayout)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.properties import StringProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout
Config.set('graphics', 'width', 360)
Config.set('graphics', 'height', 640)
kv= Builder.load_file('test5.kv')
#Define a RecycleView
class RV (RecycleView):
pass
class AwesomeApp(App):
def build(self):
return RV()
if __name__ == '__main__':
AwesomeApp().run()
kv.file kv文件
<RV>:
viewclass: 'Button'
RecycleBoxLayout:
default_size: None,100
default_size_hint: 1, None
size_hinty:None
height: self.minimum_height
orientation:'vertical'
Button:
text:"test1"
Button:
text:"test2"
Label:
text: "text3"
A couple of things are wrong here.这里有几件事是错误的。
First you're size_hinty
variable should be written size_hint_y
.首先你的size_hinty
变量应该写成size_hint_y
。
The second is that your widget properties need to added to RecycleView.data
.第二个是您的小部件属性需要添加到RecycleView.data
。
Try removing the buttons from the <RV>
class in your.kv file and adding self.data
to your RV
class in your.py file.尝试从您的 .kv 文件中的<RV>
class 中删除按钮,并将self.data
添加到您的 .py 文件中的RV
class。 That is:那是:
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'text': f'test{x}'} for x in range(1, 4)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.