如何创建kivy下拉菜单?

时间:2019-11-12 07:28:03

标签: python kivy kivy-language

我想用kivy创建一个const shows = [{ "name": "John Doe Show", "rating": 1234, "alternative_name": null, }, { "name": "Jane Doe Show", "rating": 4321, "alternative_name": "Alternate Name 2", }, { "name": "Other Show", "rating": 8842, "alternative_name": "Alternate Name 3", }]; function getAltName(oName) { const result = shows.filter(x => x.name === oName)[0]; return result.alternative_name || "Can not find your expected data."; } // this method to trims and take only data with alternative_name. function trimByAltName() { return shows.filter(x => x.alternative_name !== null); } console.log('Get alternative name only:', getAltName('Jane Doe Show')); console.log('Get Filtered Data:', trimByAltName());菜单。我的代码无法正常工作。我没有收到错误,但是看不到DropDown菜单。请帮忙。

tut10.py

DropDown

Design8.kv

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()    

1 个答案:

答案 0 :(得分:0)

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

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')