KIVY:缺少滚动条
Posted
技术标签:
【中文标题】KIVY:缺少滚动条【英文标题】:KIVY : Scroll Bar missing 【发布时间】:2020-08-06 14:58:39 【问题描述】:我正在anchorlayout 中尝试使用ScrollView。无法在相对布局中获得滚动条。请在下面找到我的示例代码。为什么滚动条不可见。
import kivy
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.factory import Factory
from kivy.core.window import Window
Builder.load_string('''
<ScrollView>:
size_hint: 1,1
DrawingSpace:
size_hint: 1,1
''')
class Main(App):
def build(self):
self.root=DrawingSpace()
return self.root
class DrawingSpace(RelativeLayout):
def __init__(self, **kwargs):
super(RelativeLayout, self).__init__(**kwargs)
self.add_widget( Button(text = 'Scroll', size_hint=(None,None), size=(50,200), pos = (100, 100)))
self.add_widget( Button(text = 'Scroll', size_hint=(None,None), size=(50,200), pos = (250, 300)))
Factory.register('DrawingSpace', cls=DrawingSpace)
if __name__ in ('__main__'):
Window.size = (200, 200)
app = Main()
app.run()
谢谢
【问题讨论】:
【参考方案1】:对于ScrollView
,在您想要滚动的方向上,您必须将size_hint
设置为None
并指定大小。我用 500 进行测试。
import kivy
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.factory import Factory
from kivy.core.window import Window
Builder.load_string('''
<ScrollView>:
size_hint: 1,1
DrawingSpace:
size_hint: 1,None
height: 500
''')
class Main(App):
def build(self):
self.root=ScrollView()
return self.root
class DrawingSpace(RelativeLayout):
def __init__(self, **kwargs):
super(RelativeLayout, self).__init__(**kwargs)
self.add_widget( Button(text = 'Scroll', size_hint=(None,None), size=(50,200), pos = (100, 100)))
self.add_widget( Button(text = 'Scroll', size_hint=(None,None), size=(50,200), pos = (250, 300)))
Factory.register('DrawingSpace', cls=DrawingSpace)
if __name__ in ('__main__'):
Window.size = (200, 200)
app = Main()
app.run()
【讨论】:
谢谢。您的解决方案有效。但是请在 KV 而不是 python 代码中进行这种更改。 你是对的。事实证明,只需对代码进行 3 处小改动即可使滚动工作。我已经更新了我的答案。以上是关于KIVY:缺少滚动条的主要内容,如果未能解决你的问题,请参考以下文章