繁体   English   中英

如何创建 kivy 下拉菜单?

[英]How do I create kivy dropdown menu?

我想在 kivy 中创建一个DropDown菜单。 我的代码不起作用。 我没有收到错误,但我看不到DropDown菜单。 请帮忙。

tut10.py

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

class drop_content(DropDown):
    pass

class Grid_5(GridLayout):
    drop = drop_content()

    def show_drop(self):
        self.drop.open

class Demo_9(App):   

    def build(self):
        return Builder.load_file("kv\Design8.kv")

if __name__ == "__main__":
    Demo_9().run()    

设计8.kv

<drop_content>:

    Label:
        text:"Drop1"
        size_hint_y: None
        height: 44

    Label:
        text:"Drop2"
        size_hint_y: None
        height: 44

Grid_5:    
    cols: 1

    Button:
        text: "Press me !!"
        size_hint: None, None
        on_press: root.show_drop()

代码中有几个问题。 在工作并尝试修复它们之后,我粘贴了我希望对你有用的代码。

tut10.py

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

Builder.load_file("kv\Design8.kv")

class drop_content(DropDown):
    pass

class Grid_5(GridLayout):
    pass

class Demo_9(App):

    def build(self):

        return Grid_5()


if __name__ == "__main__":
    Demo_9().run()

kv\Design8.kv

<Grid_5>:

    Button:
        id: btn
        text: "Press me !!"
        size_hint: None, None
        on_parent: drop_content.dismiss()
        on_release: drop_content.open(self)

    DropDown:
        id: drop_content
        on_select: btn.text = '{}'.format(args[1])

        Button:
            id: btn1
            text: 'First Item'
            size_hint_y: None
            height: 35
            on_release: drop_content.select('First Item')

        Label:
            text:"Drop2"
            size_hint_y: None
            height: 44

        Button:
            id: btn3
            text: 'Third Item'
            size_hint_y: None
            height: 35
            on_release: drop_content.select('Third Item')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM