Search code examples
pythonpython-3.xkivydropdown

How to use drop down widget in kivy


I am trying to add dropdown button to widget, but it show me only the button, without labels or textinput. After close APP it show me second widget with all stuff. Where am i making a mistake? Well thank you

My noob code:

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)

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

Solution

  • Your problem is that the code:

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

    runs your MyApp, and that does not return until MyApp closes. So the code after that does not get run until MyApp closes, and then

    runTouchApp(mainbutton)
    

    starts another App. You probably want the code that creates the DropDown inside a class somewhere (maybe MyGrid), and add the mainbutton to the gui (maybe in MyGrid).