盒式布局中的可滚动GridLayout。

问题描述 投票:1回答:3

我已经在这个问题上花了好几个小时了,尝试了我能在这里找到的所有解决方案,并尝试了一些随机的东西......我试图建立一个由顶部的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
3个回答
2
投票

你的代码是正确的,你只需要指定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()

输出。

enter image description here


0
投票

我做了上面介绍的同样的事情,但在纯Python中(不使用kv lang),但它不工作良好。我不知道哪里出了问题。

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button


class HomeScreen(Screen):
    def __init__(self, **kwargs):
        super(HomeScreen, self).__init__(**kwargs)
        box = BoxLayout()
        self.add_widget(box)

        scroll_view = ScrollView()
        box.add_widget(scroll_view)

        grid = ScrollGridLayout()
        scroll_view.add_widget(grid)

        for i in range(0, 30):
            btn = Button()
            grid.add_widget(btn)

class ScrollGridLayout(GridLayout):
    def __init__(self, **kwargs):
        super(ScrollGridLayout, self).__init__(**kwargs)
        self.orientation = "vertical"
        self.size_hint_y=None
        self.height=self.minimum_height
        self.row_default_height=100
        self.cols=1

class MyApp(App):
    def build(self):
        return HomeScreen()

def main():
    app = MyApp()
    app.run()

if __name__ == '__main__':
    main()

0
投票

我试着像@石之森一样,在纯Python3.7中重新创建@FJSevilla的应用,经过几番修改,我已经成功了!我想说的是,在纯Python3.7中,我的应用是很好用的。

# -*- 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()
© www.soinside.com 2019 - 2024. All rights reserved.