【问题标题】:Python Kivy - Adding a GridLayout to a BoxLayoutPython Kivy - 将 GridLayout 添加到 BoxLayout
【发布时间】:2018-06-03 00:13:47
【问题描述】:

我想创建一个顶部有按钮,底部有几个标签的 UI,如果标签超过了它应该可以滚动的高度。

类似这样的:

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

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,但这似乎不起作用。

如何制作可滚动列表?

【问题讨论】:

  • 是什么意思,但这似乎不起作用。?这是非常通用的,更详细的,阅读How to Ask
  • 我可以在 GridLayout 中添加一个标签,但它不会创建一个可滚动的列表。 imgur.com/lynkv3j
  • 这增加了你的问题

标签: python python-3.x kivy


【解决方案1】:

您必须将 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多