我在使用Kivy语言和Python语言的概念之间来回徘徊时遇到了麻烦。我不太擅长解释事情,而且我已经考虑过如何解释我的特定的问题,但我能想到的最好方法是:
如何使用Builder
函数实现ScrollViewApp
?
答案 0 :(得分:4)
嗯,类似
ScrollView:
size_hint: None, None
size: 500, 320
pos_hint: {'center_x': .5, 'center_y': .5}
do_scroll_x: False
GridLayout:
cols: 1
padding: 10
spacing: 10
size_hint_y: None
height: self.minimum_height
ScrollButton:
text: '1'
ScrollButton:
text: '2'
ScrollButton:
text: '3'
ScrollButton:
text: '4'
ScrollButton:
text: '5'
ScrollButton:
text: '6'
<ScrollButton@Button>
size_hint: None, None
size: 480, 40
这里,但我们真的没有办法动态创建孩子(好吧,会有方法,但它们很难看),所以我放了几个manualy,想要你会在kv中创建ScrollView和GridLayout ,然后将这些子项放在python中(使用id,如文档中所述)。
编辑:使用app和ObjectProperty
的更完整版本kv文件(scroll.kv):
ScreenManager:
Screen:
ScrollView:
size_hint: None, None
size: 500, 320
pos_hint: {'center_x': .5, 'center_y': .5}
GridLayout:
cols: 1
padding: 10
spacing: 10
height: self.minimum_height
size_hint: None, None
do_scroll_x: False
id: container
<ScrollButton>
size_hint: None, None
size: 480, 40
python文件(main.py):
from kivy.app import App
from kivy.uix.button import Button
class ScrollButton(Button):
pass
class ScrollApp(App):
def build(self):
super(ScrollApp, self).build()
container = self.root.ids.container
for i in range(30):
container.add_widget(ScrollButton(text=str(i)))
return self.root # return root does not work
if __name__ == '__main__':
ScrollApp().run()