如何在 Kivy ScrollView 中滚动 GridLayout?
Posted
技术标签:
【中文标题】如何在 Kivy ScrollView 中滚动 GridLayout?【英文标题】:How do you scroll a GridLayout inside Kivy ScrollView? 【发布时间】:2014-12-28 11:30:26 【问题描述】:目前这是我无法滚动的 kv 代码:
BoxLayout:
id: bl
orientation: 'vertical'
padding: 10, 10
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
GridLayout:
id: layout_content
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
Label:
text: 'You don''t have any downloads. Please add new download from Home screen'
如何让上面的 kv 代码可滚动?我知道 Kivy ScrollView 只接受一个孩子,而且我已经让 GridLayout 成为新 ScrollView 的孩子。但它不起作用。有什么建议吗?
【问题讨论】:
【参考方案1】:根据documentation for ScrollView,您必须至少禁用 ScrollView 的子 size_hint 之一:
<Controller>:
layout_content: layout_content
BoxLayout:
id: bl
orientation: 'vertical'
padding: 10, 10
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
ScrollView:
size: self.size
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
Label:
text: "Lorem ipsum dolor sit amet"
并绑定布局的大小以适应自身:
# main.py
class Controller(FloatLayout):
layout_content=ObjectProperty(None)
def __init__(self, **kwargs):
super(Controller, self).__init__(**kwargs)
self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
【讨论】:
几乎就是我刚才所做的! :) 谢谢.. @syntax_error? :D 是的,几乎:魔鬼在细节中,特别是 kv 的东西:) 不得不说,我有点希望 Kivy-Designer 很快成为他们的主要意图。它肯定会帮助我们轻松设计界面。不幸的是,该项目似乎处于休眠状态。我以这种方式想念 PyQt.. 你确定这是正确的吗?我收到一个错误Traceback (most recent call last): File "main.py", line 24, in <module> edlApp().run() File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 766, in run root = self.build() File "main.py", line 20, in build return Controller() File "main.py", line 16, in __init__ self.layout_content.bind(minimum_height=self.layout_content.setter('height')) AttributeError: 'NoneType' object has no attribute 'bind'
应该是ScrollView:
【参考方案2】:
解决了滚动视图在最大化时不随窗口调整长度的问题。没有 kv 的人需要它而没有
kivy.require('1.11.1')
class ScrollTest(ScrollView):
def __init__(self,**kwargs):
super().__init__(**kwargs)
Window.bind(on_maximize=self.testing)
self.size_hint = (1,None)
self.pos_hint = 'center_x':0.5,'top': 1
self.size = (Window.width,Window.height)
self.inside = GridLayout()
self.inside.cols = 1
self.inside.size_hint_y= None
self.inside.spacing = 10
self.inside.bind(minimum_height=self.inside.setter('height'))#checks when window maximized
for i in range(1,5):
self.submit = Button(text='something',size_hint_y=None, height=40)
self.submit.bind(on_press=self.wPressed)
self.inside.add_widget(self.submit)
self.add_widget(self.inside)
def wPressed(self,instance):
self.submit = Button(text='something',size_hint_y=None, height=40)
self.submit.bind(on_press=self.wPressed)
self.inside.add_widget(self.submit)
def testing(self,instance):
self.size= (Window.width,Window.height)
class MyApp(App):
def build(self):
self.screen_manager = ScreenManager()
'''Creation of login screen'''
self.login_page = ScrollTest()
screen = Screen(name= 'Login')
screen.add_widget(self.login_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
if __name__ == '__main__':
MyApp().run()
【讨论】:
从 kivy.uix.scrollview 导入 ScrollView 从 kivy.core.window 导入 Window 从 kivy.uix.gridlayout 导入 GridLayout 从 kivy.uix.button 导入按钮以上是关于如何在 Kivy ScrollView 中滚动 GridLayout?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 python kivy 中创建可滚动的 FloatLayout