在 ScrollView 中滚动 GridLayout 的内容 - Kivy
Scroll contents of GridLayout in ScrollView - Kivy
首先我会说我已经尝试了网络上涉及 kv lang 的每个示例。我一次都没有成功。
想法非常简单:当我滑动 up/down/scroll 时,ScrollView()
中 GridLayout()
的内容向上或向下滚动。
我能做的最好的事情就是在 运行 程序时让滚动条淡入视野。不幸的是无法滚动。
<Root>
grid_layout: grid_layout
ScreenManager:
...
Screen:
...
ScrollView:
GridLayout:
id: grid_layout
size_hint_y: None
cols: 1
height: self.minimum_height
<list of buttons>
在根的__init__
方法中绑定minimum_height
class(RelativeLayout):
grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))
我已按照 https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py 将其转换为 kv lang - 滚动条可见,无法滚动。还尝试了 Google 组上的每个示例,这里与使用 kv lang 相关。仍然没有滚动:\
在 Android 上使用 buildozer 和 运行 编译失败,原因不明。
如果能提供任何帮助,我将不胜感激。我现在完全一无所知
这个:
height: self.minimum_height
应该是:
minimum_height: self.height
这是不必要的:
grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))
它也不会滚动,除非内容大于滚动视图的高度:
完整代码:
from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
Builder.load_string('''
<Root>:
ScrollView:
size_hint: 1, .1
# setting the width of the scrollbar to 50pixels
bar_width: 50
# setting the color of the active bar using rgba
bar_color: 5, 10, 15, .8
# setting the color of the inactive bar using rgba
bar_inactive_color: 5, 20, 10, .5
# setting the content only to scroll via bar, not content
scroll_type: ['bars']
GridLayout:
size_hint_y: None
cols: 1
minimum_height: self.height
Button
text: 'one'
Button:
text: 'two'
Button:
text: 'three'
Button:
text: 'four'
''')
class Root(FloatLayout):
pass
class DemoApp(App):
def build(self):
return Root()
if __name__ == '__main__':
DemoApp().run()
无法滚动是由于误解了 Kivy 的 touch
处理程序。与我问题中提到的代码完全无关。
关键是 GridLayout
大于 ScrollView
,所以 GridLayout
可以在 ScrollView
内平移。
对于那些想在 ScreenManager
中仅使用 kvlang 使用 ScrollView
的人:
ScreenManager:
id: screen_manager
Screen:
manager: screen_manager
id: main_screen
name: 'main'
ScrollView:
bar_width: 4
# pos_hint defaults to 1,1 so no need to declare it
GridLayout:
size_hint_y: None
cols: 1
# you do not need to manually bind to setter('height') in
# python - perfectly possible with kv lang
# this allows for height to update depending on the
# collective heights of its child widgets
height: self.minimum_height
<----- widgets here ----->
# for scroll to show/work there must be more widgets
# then can fit root.height. If not there is no need
# for scrollview :)
首先我会说我已经尝试了网络上涉及 kv lang 的每个示例。我一次都没有成功。
想法非常简单:当我滑动 up/down/scroll 时,ScrollView()
中 GridLayout()
的内容向上或向下滚动。
我能做的最好的事情就是在 运行 程序时让滚动条淡入视野。不幸的是无法滚动。
<Root>
grid_layout: grid_layout
ScreenManager:
...
Screen:
...
ScrollView:
GridLayout:
id: grid_layout
size_hint_y: None
cols: 1
height: self.minimum_height
<list of buttons>
在根的__init__
方法中绑定minimum_height
class(RelativeLayout):
grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))
我已按照 https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py 将其转换为 kv lang - 滚动条可见,无法滚动。还尝试了 Google 组上的每个示例,这里与使用 kv lang 相关。仍然没有滚动:\
在 Android 上使用 buildozer 和 运行 编译失败,原因不明。
如果能提供任何帮助,我将不胜感激。我现在完全一无所知
这个:
height: self.minimum_height
应该是:
minimum_height: self.height
这是不必要的:
grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))
它也不会滚动,除非内容大于滚动视图的高度:
完整代码:
from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
Builder.load_string('''
<Root>:
ScrollView:
size_hint: 1, .1
# setting the width of the scrollbar to 50pixels
bar_width: 50
# setting the color of the active bar using rgba
bar_color: 5, 10, 15, .8
# setting the color of the inactive bar using rgba
bar_inactive_color: 5, 20, 10, .5
# setting the content only to scroll via bar, not content
scroll_type: ['bars']
GridLayout:
size_hint_y: None
cols: 1
minimum_height: self.height
Button
text: 'one'
Button:
text: 'two'
Button:
text: 'three'
Button:
text: 'four'
''')
class Root(FloatLayout):
pass
class DemoApp(App):
def build(self):
return Root()
if __name__ == '__main__':
DemoApp().run()
无法滚动是由于误解了 Kivy 的 touch
处理程序。与我问题中提到的代码完全无关。
关键是 GridLayout
大于 ScrollView
,所以 GridLayout
可以在 ScrollView
内平移。
对于那些想在 ScreenManager
中仅使用 kvlang 使用 ScrollView
的人:
ScreenManager:
id: screen_manager
Screen:
manager: screen_manager
id: main_screen
name: 'main'
ScrollView:
bar_width: 4
# pos_hint defaults to 1,1 so no need to declare it
GridLayout:
size_hint_y: None
cols: 1
# you do not need to manually bind to setter('height') in
# python - perfectly possible with kv lang
# this allows for height to update depending on the
# collective heights of its child widgets
height: self.minimum_height
<----- widgets here ----->
# for scroll to show/work there must be more widgets
# then can fit root.height. If not there is no need
# for scrollview :)