Search code examples
pythondrop-down-menukivy

How to get the selected value from my kivy dropdown menu


I have a dropdown menu in which I would like to know which value the user has selected so I can use it later in my program. This is my .kv code:

BoxLayout:
            orientation: 'horizontal'
            size_hint_x: 1

            Button:
                pos_hint:{'center_x': .5, 'center_y': .5}
                id: units_num_btn
                text: '0'
                size_hint_y: None
                height: 44
                on_parent: drop_player_numbers_units.dismiss()
                on_release: drop_player_numbers_units.open(self)

            DropDown:
                id: drop_player_numbers_units
                on_select: units_num_btn.text = '{}'.format(args[1])
                on_select: app.return_player_numbers()

                Button:
                    id: units_num_btn_1
                    text: '1'
                    size_hint_y: None
                    height: 35
                    on_release: drop_player_numbers_units.select('1')


                Button:
                    id: units_num_btn_2
                    text: '2'
                    size_hint_y: None
                    height: 35
                    on_release: drop_player_numbers_units.select('2')

and so on.

My .py code is here:

class drop_content(DropDown):
    pass

class PlayerScreen(Screen):
    pass

class TheApp(App):

    def build(self):
        sm = ScreenManager()
        sm.add_widget(PlayerScreen(name='player_setup'))
        sm.current = 'player_setup'

        return sm

def main():
    Builder.load_file('menu.kv')
    app = TheApp()
    app.run()

if __name__ == '__main__':
    main()

I have previously used a function such as this:

#       .py example
def return_text(self):
        text = self.root.get_screen('screen').ids.specific_id.text
        print(text)
# .kv example

TextInput:
     id: input
     text: "2"
     on_text: app.return_text()

which did return text using a Textinput type in my .kv file. I know it doesn't work for the dropdown menu since the text is not inputted in the same way. Do you know how I would do this?

Thanks in advance


Solution

  • I have found a way that I think is simpler than the current answer provided. This method uses that fact that the text on the button used to initiate the drop down changes as another button is selected. This is to show the user what they have selected. Since I want to know the text the user has selected from the drop down menu, and this is in fact the same thing that is updated on the initial button, I can just read the text from the button to obtatin my result like so:

    (in .kv file)

    <player_setup>
    BoxLayout:
            orientation: 'horizontal'
            size_hint_x: 0.2
            pos_hint: {'x':.4, 'y':0}
    
            Button:
                pos_hint:{'center_x': .5, 'center_y': .5}
                id: tens_num_btn
                text: '0'
                size_hint_y: None
                # size_hint_x: 0.1
                height: 44
                on_parent: drop_player_numbers_tens.dismiss()
                on_release: drop_player_numbers_tens.open(self)
    
            DropDown:
                id: drop_player_numbers_tens
                on_select:
    #######
                    tens_num_btn.text = '{}'.format(args[1])   
    # this line here ^ is the one that re-formats the text of the button above to
    #  the selected text
    
                    app.return_player_numbers()
                max_height: 120
    

    (.py file)

    
    def return_player_numbers(self):
    
            player_number = self.root.get_screen('player_setup').ids.tens_num_btn.text
            return player_number
    
    
    

    This also allows me to concatenate multiple dropdown results using a single function, however it is menu specific. In my case, this works better for me