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

问题描述 投票:0回答:1

我正在尝试将下拉按钮添加到窗口小部件,但仅向我显示该按钮,没有标签或文本输入。关闭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。文件:

<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()
python python-3.x kivy dropdown
1个回答
1
投票

您的问题是代码:

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

运行您的MyApp,并且直到MyApp关闭后才会返回。因此,此后的代码要到MyApp关闭后才能运行,然后

runTouchApp(mainbutton)

启动另一个App。您可能想要在某个地方的某个类(可能是DropDown)中的类中创建MyGrid的代码,然后将mainbutton添加到gui(也许在MyGrid中)。

© www.soinside.com 2019 - 2024. All rights reserved.