Python KIVY:递归DropDown

时间:2017-12-19 19:15:12

标签: python kivy dropdown

我想创建一个DropDown,其中包含3个其他DropDowns,每个DropDowns包含3个按钮。

我希望第一个DropDown再打开三个DropDowns,当我们点击其中一个DropDowns时,会出现三个按钮,并且在任何情况下都会出现一个按钮,下面没有显示错误。当您第二次单击下拉列表时,它会隐藏其子窗口小部件。

我用这种方式创建一个下拉列表:

的.py:

from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import StringProperty


class MyScreenManager(ScreenManager):
    button_text = StringProperty('Show possibilities')

    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        self.dropdown = CustomDropDown(self)

    def open_drop_down(self, widget):            
        self.dropdown.open(widget)

class MyScreen(Screen):
    def __init__(self, **kwargs):
        super(MyScreen, self).__init__(**kwargs)

class CustomDropDown(DropDown):
    def __init__(self, screen_manager, **kwargs):
        super(CustomDropDown, self).__init__(**kwargs)
        self.sm = screen_manager

    def on_select(self, data):
        self.sm.button_text = data

Builder.load_file("debug.kv")

class MyAppli(App):
    def build(self):
        return MyScreenManager()

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

.kv:

<MyScreenManager>:

    MyScreen:

        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'top'

            Button:

                text: root.button_text
                size:(200,50)
                size_hint:(None,None)
                on_release: root.open_drop_down(self)

<CustomDropDown>:
    Button:
        text:"Item 1"
        size:(200,50)
        size_hint:(None,None)
        on_release: root.select("ONE")
    Button:
        text:"Item 2"
        size:(200,50)
        size_hint:(None,None)
        on_release: root.select(self.text)
    Button:
        text:"Item 3"
        size:(200,50)
        size_hint:(None,None)
        on_release: root.select("THREE")

1 个答案:

答案 0 :(得分:0)

添加另一个自定义下拉类并添加按钮方法以打开/关闭第二个下拉列表。 open_drop2()方法执行打开/关闭。为每个padding添加了Button,以尝试显示级联。可以使用相同的方案完成额外的级联,即添加由CustomDropDown3中的按钮打开的CustomDropDown2类。请注意,需要kv文件中的dismiss_on_select: False才能确保is2Displayed属性更新正确。

的.py:

from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder


class MyScreenManager(ScreenManager):
    button_text = StringProperty('Show possibilities')

    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        self.dropdown = CustomDropDown1(self)

    def open_drop_down(self, widget):
        self.dropdown.open(widget)

class MyScreen(Screen):
    def __init__(self, **kwargs):
        super(MyScreen, self).__init__(**kwargs)


class CustomDropDown1(DropDown):
    def __init__(self, screen_manager, **kwargs):
        super(CustomDropDown1, self).__init__(**kwargs)
        self.sm = screen_manager
        self.dropdown2 = CustomDropDown2(self.sm)
        self.is2Displayed = False

    def on_select(self, data):
        self.sm.button_text = data

    def open_drop2(self):
        if not self.is2Displayed:
            self.dropdown2.open(self)
            self.is2Displayed = True
        else:
            self.dropdown2.dismiss()
            self.is2Displayed = False

class CustomDropDown2(DropDown):
    def __init__(self, screen_manager, **kwargs):
        super(CustomDropDown2, self).__init__(**kwargs)
        self.sm = screen_manager

    def on_select(self, data):
        self.sm.button_text = data

Builder.load_file("debug.kv")

class MyAppli(App):
    def build(self):
        return MyScreenManager()

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

.kv:

<MyScreenManager>:
    MyScreen:
        FloatLayout:
            Button:
                text: root.button_text
                size: (200, 50)
                size_hint:(None,None)
                pos_hint: {'center_x': 0.5, 'top': 1.0}
                on_release: root.open_drop_down(self)

<CustomDropDown1>:
    padding: [0,0,0,0]
    Button:
        text:"Item 1"
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.select(self.text)
    Button:
        text:"Item 2"
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.open_drop2()
    Button:
        text:"Item 3"
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.select(self.text)

<CustomDropDown2>:
    dismiss_on_select: False
    Button:
        text:"Item 1,2"
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (50,0)
        on_release: root.select(self.text)
    Button:
        text:"Item 2,2"
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (50,0)
        on_release: root.select(self.text)
    Button:
        text:"Item 3,2"
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (50,0)
        on_release: root.select(self.text)