我是新手,所以我正在开发一个测试应用程序。我想用ScrollView创建一个屏幕,并想在ScrollView的“行”中添加多个内容,一个文本(描述)和一个图像。 我已经尝试过这种方式:
class PresentUploadedData(Screen):
container = ObjectProperty(None)
def __init__(self, **kwargs):
super(PresentUploadedData, self).__init__(**kwargs)
Clock.schedule_once(self.setup_scrollview, 1)
def setup_scrollview(self, dt):
self.container.bind(minimum_height=self.container.setter('height'))
self.add_text_inputs()
def add_text_inputs(self):
for x in range(30):
self.container.add_widget(Label(text="Label {}".format(x), size_hint_y=None, height=40, color= [128,0,0,1]))
self.container.add_widget(Image(source='test.jpg', size_hint=(None, None,), width=50, height=50))
与此.kv
文件:
<PresentUploadedData>:
name: "presentupload"
message: send
container: container
canvas:
Color:
rgba: 1,1,1,1
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 3
cols: 1
spacing: 5
padding: 5
font_name: "Calibri"
background_color: 1,1,1, 1
ScrollView:
background_color: 1,1,1, 1
size_hint: (1, .9)
bar_width: 10
bar_color: 128,0,0,0.7
bar_inactive_color: 128,0,0,1
effect_cls: "ScrollEffect"
scroll_type: ['bars', 'content']
color: 128,0,0,1
StackLayout:
id: container
size_hint_y: None
color: 128,0,0,1
GridLayout:
cols: 2
BoxLayout:
spacing: 5
size_hint: .7, .1
Button:
text: "< BACK"
id: send
color: 0, 0, 0, 1
background_color: .88,.88,.88, 1
size_hint: .2, 1
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
但是我有ScrollView accept only one widget
异常。
为什么得到这个,如何解决呢?
答案 0 :(得分:0)
.kv
文件中存在错误:
GridLayout:
cols: 2
这没用。
可以在for
循环中再添加一个for
来解决该问题:
row = [Label(text="Label {}".format(x), size_hint_y=None, height=50, color= [128,0,0,1]), Image(source='test.jpg', size_hint=(None, None,), width=50, height=50)]
for r in row:
self.container.add_widget(r)
但这也没有使事物彼此靠近,只是让它们在那里。