Kivy如何在GridLayout中创建下拉列表

时间:2019-03-20 02:03:52

标签: kivy

我有2个文件,其中一个 py 和另一个 kv ,正如标题所述,我不知道如何添加下拉列表。

---> main.py

from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout

class MyGridLayout(GridLayout):
    pass

class LayoutsApp(App):
    def build(self):
        return MyGridLayout()


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

---> layouts.kv

<MyGridLayout>:
    rows: 2
    id: main_win
    Dropdown:
        id: dropdown
        Button:
            id: btn1
            text: 'option 1'
        Button:
            id: btn2
            text: 'option 2'
    BoxLayout:

    BoxLayout:

编译时,它将为该部分生成错误。致电下拉列表的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

问题3

  

调用Button id:btn2,我该怎么办? -我的意思是:上课   MyGridLayout(GridLayout):

解决方案3

Python脚本

  • 添加导入语句:from kivy.uix.dropdown import DropDownfrom kivy.properties import ObjectProperty
  • 实施class CustomDropDown(DropDown):
  • 在MyGridLayout类中,在类级别声明一个ObjectProperty(例如dropdown = ObjectProperty(None);实现方法dropdown_open(self):;实例化CustomDropDown类并将其分配给self.dropdown,依此类推。请参见代码段详细信息。

kv文件

  • 删除self.dismiss()
  • 用类规则<CustomDropdown@DropDown>:替换动态类<CustomDropDown>:
  • rows: 2替换为rows: 3
  • Factory.CustomDropdown().open(self)替换为root.dropdown_open(self)

示例3

main.py-代码段

from kivy.uix.dropdown import DropDown
from kivy.properties import ObjectProperty


class CustomDropDown(DropDown):
    pass


class MyGridLayout(GridLayout):
    dropdown = ObjectProperty(None)

    def dropdown_open(self, instance):
        self.dropdown = CustomDropDown()
        self.dropdown.open(instance)
        print(self.dropdown.ids.btn2.text)

layouts.kv-片段

<CustomDropDown>:
    id: dropdown
    on_select:
        app.root.ids.btn.text = '{}'.format(args[1])
...
<MyGridLayout>:
    rows: 3
    id: main_win

    Button:
        id: btn
        text: 'Press'
        size_hint_y: None
        height: '48dp'
        on_release:
            root.dropdown_open(self)

输出3

Printed btn2.text

问题2

  

当我添加这些命令时,它不起作用:BoxLayout:方向:   'vertical'size_hint:(.9,.9)BoxLayout:方向:'vertical'   size_hint_y:0.5

注意

您可能需要将行从2增加到3。

示例2

  • 添加了两个BoxLayout的配置
  • 为说明起见,为两个BoxLayout的画布添加了颜色

layouts.kv

#:kivy 1.10.1
#:import Factory kivy.factory.Factory

<CustomDropdown@DropDown>:
    id: dropdown
    on_select:
        app.root.ids.btn.text = '{}'.format(args[1])
        self.dismiss()

    Button:
        id: btn1
        text: 'option 1'
        size_hint_y: None
        height: '48dp'
        on_release:
            dropdown.select(btn1.text)

    Button:
        id: btn2
        text: 'option 2'
        size_hint_y: None
        height: '48dp'
        on_release:
            dropdown.select(btn2.text)

<MyGridLayout>:
    rows: 2
    id: main_win

    Button:
        id: btn
        text: 'Press'
        on_release: Factory.CustomDropdown().open(self)
        size_hint_y: None
        height: '48dp'

    BoxLayout:
        orientation: 'vertical'
        size_hint: (.9,.9)
        canvas.before:
            Color:
                rgba: 1, 0, 0, 1    # red
            Rectangle:
                pos: self.pos
                size: self.size

    BoxLayout:
        orientation: 'vertical'
        size_hint_y: 0.5
        canvas.before:
            Color:
                rgba: 0, 0, 1, 1    # blue
            Rectangle:
                pos: self.pos
                size: self.size

输出#2

App Startup DropDown List DropDown Selected

解决方案#1

DropDown小部件类似于Popup小部件,即它们是特殊的小部件。

  

Popup / DropDown是一个特殊的小部件。请勿尝试将其作为其他孩子添加   小部件。如果您这样做,将像普通小部件一样处理Popup / DropDown   不会隐藏在后台。

因此,创建一个具有DropDown小部件继承的动态类,并使用Factory实例化该类。

示例

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout


class MyGridLayout(GridLayout):
    pass


class LayoutsApp(App):
    def build(self):
        return MyGridLayout()


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

layouts.kv

#:kivy 1.10.1
#:import Factory kivy.factory.Factory

<CustomDropdown@DropDown>:
    id: dropdown
    on_select:
        app.root.ids.btn.text = '{}'.format(args[1])
        self.dismiss()

    Button:
        id: btn1
        text: 'option 1'
        size_hint_y: None
        height: '48dp'
        on_release:
            dropdown.select(btn1.text)

    Button:
        id: btn2
        text: 'option 2'
        size_hint_y: None
        height: '48dp'
        on_release:
            dropdown.select(btn2.text)

<MyGridLayout>:
    rows: 2
    id: main_win

    Button:
        id: btn
        text: 'Press'
        on_release: Factory.CustomDropdown().open(self)
        size_hint_y: None
        height: '48dp'

    BoxLayout:

    BoxLayout:

输出

App Startup DropDown opened DropDown list selected