如何在kivy中使用下拉小部件

How to use drop down widget in kivy

我正在尝试向小部件添加下拉按钮,但它只显示按钮,没有标签或文本输入。关闭 APP 后,它会向我显示第二个包含所有内容的小部件。 我在哪里犯了错误? 好的谢谢

我的菜鸟代码:

main.py:

class MyGrid(FloatLayout):
    name = ObjectProperty(None)
    email = ObjectProperty(None)
    psc = ObjectProperty(None)


def btn(self):
    self.clear_btn()

def clear_btn(self):
    self.email.text = ""

class MyApp(App): # <- Main Class
    def build(self):
        return MyGrid()


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

dropdown = DropDown()
for index in range(10):

    btn = Button(text='Value %d' % index, size_hint_y=None, height=44)

    btn.bind(on_release=lambda btn: dropdown.select(btn.text))

    dropdown.add_widget(btn)


mainbutton = Button(text='Hello', size_hint=(None, None))

mainbutton.bind(on_release=dropdown.open)

dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))

runTouchApp(mainbutton)

我的kv.file:

<MyGrid>:
    Label:
        text: "Email: "

    TextInput:
        id: email
        multiline:False


    Button:
        size_hint: 0.3, 0.1
        pos_hint: {"x":0.5, "top":0.11}
        text:"Send"
        on_press: root.btn()

你的问题是代码:

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

运行 是您的 MyApp,并且 return 直到 MyApp 关闭。所以之后的代码直到MyApp关闭才得到运行,然后

runTouchApp(mainbutton)

开始另一个 App。您可能想要在某处(可能是 MyGrid)的 class 中创建 DropDown 的代码,并将 mainbutton 添加到 gui(可能是 MyGrid 中) .