Python, Kivy. Get text from dynamically created buttons with a dropdown

后端 未结 1 463
礼貌的吻别
礼貌的吻别 2021-01-27 07:14

I am dynamically creating buttons and dynamically creating a dropdown for each button. when the button is selected the text value updates on the main button. I want to get the

相关标签:
1条回答
  • 2021-01-27 07:31

    Use on_select event to pass the selected text to the method. Please refer to the snippets and example for details.

    Snippets - kv file

    <CustomDropDown>:
        on_select:
            app.root.ids.Notes.ids.mainbutton.text = '{}'.format(args[1])
            app.root.Run_Draws_Test(args[1])
    

    Example

    main.py

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.uix.dropdown import DropDown
    from kivy.uix.button import Button
    from kivy.core.window import Window
    
    
    Window.size = (800, 480)
    
    
    class CustomDropDown(DropDown):
    
        def __init__(self, **kwargs):
            super(CustomDropDown, self).__init__(**kwargs)
            self.add_buttons()
    
        def add_buttons(self):
            for index in range(10):
                # When adding widgets, we need to specify the height manually
                # (disabling the size_hint_y) so the dropdown can calculate
                # the area it needs.
    
                btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
    
                # for each button, attach a callback that will call the select() method
                # on the dropdown. We'll pass the text of the button as the data of the
                # selection.
                btn.bind(on_release=lambda btn: self.select(btn.text))
    
                # then add the button inside the dropdown
                self.add_widget(btn)
    
    
    class Notes(Screen):
        pass
    
    
    class MyScreenManager(ScreenManager):
    
        def Run_Draws_Test(self, value):
            print(value)
    
    
    class TestApp(App):
        title = "Kivy Drop-Down List Demo"
    
        def build(self):
            return MyScreenManager()
    
    
    if __name__ == '__main__':
        TestApp().run()
    

    test.kv

    #:kivy 1.10.0
    #:import Factory kivy.factory.Factory
    
    <CustomDropDown>:
        on_select:
            app.root.ids.Notes.ids.mainbutton.text = '{}'.format(args[1])
            app.root.Run_Draws_Test(args[1])
    
    
    <Notes>:
        orientation: "vertical"
    
        FloatLayout:
            size_hint: None, None
    
            canvas.before:
                Color:
                    rgba: 1, 1, 0, 1
    
            Button:
                id: mainbutton
                text: "Menu name"
                font_size: 20
                size_hint: None, None
                size: 150, 50
                pos: 20,400
                on_release: Factory.CustomDropDown().open(self)
    
    <MyScreenManager>:
        canvas.before:
            Color:
                rgba: 0.5, 0.5, 0.5, 0.5
            Rectangle:
                pos: 0,0
                size: 800, 480
        Notes:
            id:Notes
            name: 'Notes'
    

    Output

    0 讨论(0)
提交回复
热议问题