在kivy中添加下拉项之间的间距

时间:2017-04-18 18:23:47

标签: python kivy kivy-language

我的问题在于访问container的{​​{1}}属性,默认情况下为DropDown且包含它的子级。

简单应用:

GridLayout

我尝试使用from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.dropdown import DropDown from kivy.uix.button import Button from kivy.uix.label import Label class TestApp(App): def build(self): root = BoxLayout(orientation='vertical') dropdown = DropDown() for i in range(3): dropdown.add_widget(Button( text=str(i), size_hint_y=None )) # add 3 buttons to dropdown dropdown.container.bind(spacing=8) # this line does not work dropdown_button = Button(size_hint_y=.2, text='Open DropDown') dropdown_button.bind(on_release=dropdown.open) root.add_widget(dropdown_button) root.add_widget(Label()) # empty space under button return root TestApp().run() 方法,但没有结果。没有设置缩进。我也希望看到kivy语言的解决方案,因为我想使用bind函数来设置间距,并且将参数传递给python文件并不是很方便。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

而是使用bind,您应该直接设置值:

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.widget import Widget


class TestApp(App):
    def build(self):
        root = BoxLayout(orientation='vertical')
        dropdown = DropDown()

        for i in range(3):
            dropdown.add_widget(Button(
                text=str(i),
                size_hint_y=None
            )) # add 3 buttons to dropdown

        dropdown.container.spacing = 10
        dropdown.container.padding = (0, 10, 0, 0)

        dropdown_button = Button(size_hint_y=.2, text='Open DropDown')
        dropdown_button.bind(on_release=dropdown.open)

        root.add_widget(dropdown_button)
        root.add_widget(Widget()) # empty space under button
        return root

TestApp().run()

不直接使用自定义容器类。你可以这样做,但它的hacky和丑陋:

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.widget import Widget

from kivy.lang import Builder

Builder.load_string('''
<MyContainer>:
    # copied from kivy.uix.dropdown._grid_kv
    size_hint_y: None
    height: self.minimum_size[1]
    cols: 1
    # custom settings
    spacing: 10
    padding: (0, 10, 0, 0)
''')

class MyContainer(GridLayout):
    pass


class TestApp(App):
    def build(self):
        root = BoxLayout(orientation='vertical')

        container = MyContainer()
        dropdown = DropDown(container=container)
        super(DropDown, dropdown).add_widget(container)
        dropdown.on_container(dropdown, container)

        for i in range(3):
            dropdown.add_widget(Button(
                text=str(i),
                size_hint_y=None
            )) # add 3 buttons to dropdown

        dropdown_button = Button(size_hint_y=.2, text='Open DropDown')
        dropdown_button.bind(on_release=dropdown.open)

        root.add_widget(dropdown_button)
        root.add_widget(Widget()) # empty space under button
        return root

TestApp().run()

所以我说它更干净,将自定义间距类作为Widget的子类来填充按钮之间的空间:

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.widget import Widget

from kivy.lang import Builder

Builder.load_string('''
<DropDownSpacing>:
    size_hint_y: None
    height: 20
''')

class DropDownSpacing(Widget):
    pass


class TestApp(App):
    def build(self):
        root = BoxLayout(orientation='vertical')
        dropdown = DropDown()

        for i in range(3):
            dropdown.add_widget(DropDownSpacing())

            dropdown.add_widget(Button(
                text=str(i),
                size_hint_y=None
            )) # add 3 buttons to dropdown

        dropdown_button = Button(size_hint_y=.2, text='Open DropDown')
        dropdown_button.bind(on_release=dropdown.open)

        root.add_widget(dropdown_button)
        root.add_widget(Widget()) # empty space under button
        return root

TestApp().run()

这与您在主BoxLayout中执行的操作相同,但我更喜欢直接使用Widget课程,而不是Label而没有文字。