【问题标题】:Scrollable GridLayout in a BoxlayoutBoxlayout 中的可滚动 GridLayout
【发布时间】:2017-07-17 03:14:42
【问题描述】:

我已经在这里工作了好几个小时了,尝试了我在这里能找到的所有解决方案,并尝试了一些随机的东西...... 我正在尝试构建一个由顶部的 3 个按钮组成的布局,然后是一个可滚动的 GridLayout 或 BoxLayout。我只是不知道出了什么问题......我已经阅读了一个回复“绑定布局的大小以适应自身:”但我正在使用屏幕管理并且无法弄清楚如何通过我的代码设置来做到这一点

<HomeScreen>:
BoxLayout:
    orientation: "vertical"
    BoxLayout:
        size_hint: 1,.1
        orientation: "horizontal"
        Button:
            text:"1"
        Button:
            text:"2"
        Button:
            text:"3"
    ScrollView:
        GridLayout:
            orientation: "vertical"
            size_hint_y: None
            row_default_height: 60
            cols:1

            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:
            Button:

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    您的代码是正确的,您只需要指定 GridLayout 的高度。你可以使用height: self.minimum_height

    可重现的例子:

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.lang import Builder
    
    kv_text = '''
    
    <MyScreenManager>:
        HomeScreen:
    
    <HomeScreen>:
        BoxLayout:
            orientation: "vertical"
            BoxLayout:
                size_hint: 1,.1
                orientation: "horizontal"
                Button:
                    text:"1"
                Button:
                    text:"2"
                Button:
                    text:"3"
            ScrollView:
                GridLayout:
                    orientation: "vertical"
                    size_hint_y: None
                    height: self.minimum_height  #<<<<<<<<<<<<<<<<<<<<
                    row_default_height: 60
                    cols:1
    
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
                    Button:
    '''
    
    class MyScreenManager(ScreenManager):
        pass
    
    class HomeScreen(Screen):
        pass
    
    class MyApp(App):
        def build(self):
            return HomeScreen()
    
    def main():
        Builder.load_string(kv_text)
        app = MyApp()
        app.run()
    
    if __name__ == '__main__':
        main()
    

    输出:

    【讨论】:

    • 我发誓我试过了......或者类似的东西......我想......大声笑非常感谢你!现在,那行“height: self.minimum_height”将 Grid 设置为正确容纳所有按钮所需的最小高度?
    • 是的,没错。 self.minimum_height 是其中包含所有子小部件的最小高度。 @Lemiwinks
    • 对于任何想要水平而不是垂直滚动的ScrollView的人,请改用width: self.minimum_width
    【解决方案2】:

    我尝试像@Ishinomori 一样在纯Python3.7 中重新创建@FJSevilla 的应用程序。 经过一些更改,我已经完成了!

    # -*- coding: utf-8 -*-
    # Kivy
    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.scrollview import ScrollView
    from kivy.uix.button import Button
    from kivy.core.window import Window
    
    
    class HomeScreen(BoxLayout):
        def __init__(self, **kwargs):
            # Initiate Box Layout and change orientation to vertical
            super().__init__(**kwargs)
            self.orientation = "vertical"
    
            # Top Bar with Buttons "1", "2" & "3"
            self.top_bar = BoxLayout(orientation="horizontal", size_hint=(1, .1))
            self.top_bar.add_widget(Button(text="1"))
            self.top_bar.add_widget(Button(text="2"))
            self.top_bar.add_widget(Button(text="3"))
    
            # Create the Gridlayout for the Scroll View and add height bounding
            self.contend_scroll_view = GridLayout(size_hint_y=None, row_default_height=60, cols=1)
            self.contend_scroll_view.bind(minimum_height=self.contend_scroll_view.setter('height'))
    
            # 30 Dummy Buttons (real data here!)
            for _ in range(30):
                self.contend_scroll_view.add_widget(Button())
    
            # Add the contend to the Scroll View
            self.scroll_view = ScrollView()
            self.scroll_view.add_widget(self.contend_scroll_view)
    
            # Add the two Widgets to Home Screen
            self.add_widget(self.top_bar)
            self.add_widget(self.scroll_view)
    
    class MyApp(App):
        def build(self):
            return HomeScreen()
    
    
    if __name__ == '__main__':
        # Only runs if file is executed directly, but not if importet
        MyApp().run()
    

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 2016-03-07
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多