Search code examples
pythonkivykivy-languagepython-class

Kivy: Update TextInput after picking value from Spinner or Dropdown


Im trying to create GUI with kivy but can't figure this.Created Popup screen and trying to pick value from dropbox and add this value to my TextInput.

test.py:

    from kivy.app import App
    from kivy.uix.textinput import TextInput
    from kivy.uix.dropdown import DropDown
    from kivy.uix.spinner import Spinner
    from kivy.uix.screenmanager import ScreenManager,Screen
    from kivy.uix.popup import Popup
    from kivy.properties import ObjectProperty
    
    class MyDropDown(DropDown):
        def on_select(self, data):
            print('Selected value:',data)
            #Lets add this data to TextInput ?
            MyTextInput.text = data
            #Lets check is that actual text ?
            print('MyTextInput.text is:',MyTextInput.text)
            #HOW CAN I ADD THIS TEXT TEXTINPUT AFTER THAT?
    
    class MySpinner(Spinner):
        dropdown_cls = ObjectProperty(MyDropDown)
    
    class MyTextInput(TextInput):
        pass
    
    class MyTestPopup(Popup):
        my_popup_spinner = ObjectProperty()
        my_textinput_id = ObjectProperty()
    
    class TestPage(Screen):
        MypagePopup = ObjectProperty()
        def open_my_popup(self, *args):
            #Lets create my popup
            self.MypagePopup = MyTestPopup()
            self.MypagePopup.my_popup_spinner.values = ['Test1','Test2','Test3','Test4','Test']
            self.MypagePopup.open()
    
    class TestPageManager(ScreenManager):
        pass
    
    class test(App):
        def build(self):
           return TestPageManager()
    
    if __name__=='__main__':
        test().run()

test.kv:

<TestPageManager>:
    TestPage:
        name: 'mainpagename'
<MySpinner>:
    text: 'Pick Value'
<MyDropDown>:
    values: ['Test1','Test2','Test3','Test4','Test5']
<MyTextInput>:
<MyTestPopup>:
    my_popup_spinner : my_popup_spinner
    my_textinput_id : my_textinput_id
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Testing Label Area..'
            pos: self.pos
            size: self.size
        MyTextInput:
            id: my_textinput_id
        MySpinner:
            id: my_popup_spinner
        Button:
            text: 'Done'
            on_release:
                print('MyTextInput id is :',my_textinput_id.text)
<TestPage>:
    Button:
        text: 'Open Popup'
        on_release: root.open_my_popup()

As you can see I created like that GUI.

But can't update TextInput after pick from spinner.How can i update this textinput after pick value from dropbox.I can't figure it out.I think i correctly describe my problem. Thanks for reading and answering.


Solution

  • kv = "Spinner:

            on_text:
    
                  my_textinput_id.text = my_popup_spinner.text"
    

    did this in my kv string for Builder.load_string(kv)and it works for me you can try and implement it in you code example and see if it works.

    set the id.text of the textinput = to my spinner id.text in the on_text: event of the Spinner