-2

I am trying to add a dropdown list using kv file. I have managed to create it but when i click on it the drop down opens and closes immediately without me selecting or clicking anywhere

How do i stop this

Button:
    id: btn
    text: 'Press'
    on_release: 
        if not root.state: dropdown.open(self); root.state = False
        else: dropdown.dismiss(); root.state = False
    size_hint: 0.2,0.05
    height: '48dp'


DropDown:

    id: dropdown
    on_parent: self.dismiss()
    on_select: btn.text = '{}'.format(args[1]); root.state = False

    Button:
        text: 'First Item'
        size_hint_y: None
        height: '48dp'
        on_release: dropdown.select('First Item')

    Label:
        text: 'Second Item'
        size_hint_y: None
        height: '48dp'

    Button:
        text: 'Third Item'
        size_hint_y: None
        height: '48dp'
        on_release: dropdown.select('Third Item')
Allan
  • 21
  • 4
  • You could add some code so we can try to help – rekiem87 Feb 07 '20 at 16:33
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you’ve written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – Alessandro Mandelli Feb 07 '20 at 16:34
  • what is `on_parent: self.dismiss()` intended to do? – John Anderson Feb 07 '20 at 17:41
  • I was working on a project and i got that code but when i remove it the drop down opens even without pressing the parent button. It closes when i click outside then the parent button starts working properly as intended – Allan Feb 07 '20 at 17:53

1 Answers1

0

If you're talking about MDDropDownMenu - it is working another way and accepts list of dicts for the buttons. Here's the example:

In kv file:

                MDRaisedButton:
                    text: app.groupstrace
                    size_hint_x: 0.15
                    # here you call drop down menu:
                    on_release: MDDropdownMenu(items=app.groupsdict, width_mult=3, background_color=[1,0.5,0,1]).open(self)
                    md_bg_color: [1,0.5,0,1]

In .py file:

groupsdict = [
            {
                "viewclass": "MDMenuItem",
                "text": "Yourtext",
                # no parentheses after function name!
                "callback": self.function1,
            },
            {
                "viewclass": "MDMenuItem",
                "text": "Yourtext",
                # in case you want label, just don't set a callback
                "callback": None,
            }
            # and so on
        ]
Lothric
  • 1,283
  • 2
  • 6
  • 11