Python Kivy - 将GridLayout添加到BoxLayout

时间:2018-06-02 15:47:56

标签: python python-3.x kivy

我想在顶部创建一个带有按钮的UI,在底部创建一些标签,如果标签超出高度,则应该可滚动。

这样的事情:

Basic Layout

到目前为止,这是我的代码:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout

class MyApp(App):

    main_layout = BoxLayout(orientation='vertical')
    top_layout = BoxLayout(orientation='horizontal')

    scrollView = ScrollView()
    gridLayout = GridLayout()
    gridLayout.cols = 1
    gridLayout.minimum_height = 10
    gridLayout.padding = [0, 0, 0, 0]
    scrollView.add_widget(gridLayout)

    main_layout.add_widget(top_layout)
    main_layout.add_widget(scrollView)

    def btn_create(self, instance):
        self.gridLayout.add_widget(Label(text='test'))

    def btn_edit(self, instance):
        pass

    def btn_delete(self, instance):
        pass

    def build(self):
        self.top_layout.size_hint=(1, .1)

        # Button 'Erstellen'
        btnCreate = Button()
        btnCreate.text = 'Erstellen'
        btnCreate.bind(on_press=self.btn_create)

        # Button 'Bearbeiten'
        btnEdit = Button()
        btnEdit.text = 'Bearbeiten'
        btnEdit.bind(on_press=self.btn_edit)

        # Button 'Löschen'
        btnDelete = Button()
        btnDelete.text = 'Löschen'
        btnDelete.bind(on_press=self.btn_delete)

        self.top_layout.add_widget(btnCreate)
        self.top_layout.add_widget(btnEdit)
        self.top_layout.add_widget(btnDelete)

        return self.main_layout

if __name__ == '__main__':
    MyApp().run()

我在ScrollView中添加了一个GridLayout,但这似乎不起作用。

如何制作可滚动列表?

1 个答案:

答案 0 :(得分:0)

您必须将GridLayout的size_hint_y设置为None,以使高度不依赖于ScrollView,并且大小最小等于GridLayout的大小。另一方面,Label必须将size_hint_y设置为None,以使高度不依赖于GridLayout。

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout

class MyApp(App):

    main_layout = BoxLayout(orientation='vertical')
    top_layout = BoxLayout(orientation='horizontal')

    scrollView = ScrollView()
    gridLayout = GridLayout(size_hint_y=None)

    gridLayout.cols = 1
    gridLayout.padding = [0, 0, 0, 0]
    gridLayout.bind(minimum_height=gridLayout.setter('height'))
    scrollView.add_widget(gridLayout)

    main_layout.add_widget(top_layout)
    main_layout.add_widget(scrollView)

    def btn_create(self, instance):
        self.gridLayout.add_widget(Label(text='test', size_hint_y=None))

    def btn_edit(self, instance):
        pass

    def btn_delete(self, instance):
        pass

    def build(self):
        self.top_layout.size_hint=(1, .1)

        # Button 'Erstellen'
        btnCreate = Button()
        btnCreate.text = 'Erstellen'
        btnCreate.bind(on_press=self.btn_create)

        # Button 'Bearbeiten'
        btnEdit = Button()
        btnEdit.text = 'Bearbeiten'
        btnEdit.bind(on_press=self.btn_edit)

        # Button 'Löschen'
        btnDelete = Button()
        btnDelete.text = 'Löschen'
        btnDelete.bind(on_press=self.btn_delete)

        self.top_layout.add_widget(btnCreate)
        self.top_layout.add_widget(btnEdit)
        self.top_layout.add_widget(btnDelete)

        return self.main_layout

if __name__ == '__main__':
    MyApp().run()