how to get data of user input from a for loop to python file in kivymd

319 views Asked by At

i am making an app to multiply 2 matrix. for that i created the user interface but cant figure it out how to get the user input in my python file, so i can do operations there.

here's my kivy file

#: import MDTextField kivymd.uix.textfield.MDTextField
<MyApp>:    
    NavigationLayout:
        ScreenManager:
            Screen:
                name: "screen1"
                GridLayout:
                    cols: 4
                    padding: 30
                    spacing: 20
                    size: root.width * 0.4, root.height * 0.8
                    row_force_default: True
                    row_default_height: 30
                    pos_hint: {'center_x': 0.5,'center_y':0.55}
                    size_hint: (None, None)
                    size: self.minimum_size
                    top: self.height
                    on_parent:
                        for i in range(16): self.add_widget(MDTextField(hint_text= 'sc', helper_text= 'hello', size_hint_x= None, width = 40))

                MDRectangleFlatButton:
                    text: 'back'
                    pos_hint: {'center_x': 0.5, 'center_y': 0.4}
                    on_release:
                        app.find_multiply()

since my MDTextField is inside a for loop, so i can't use an id there because from that i will get all 16 text field with the same id. how do i get my all 16 textfield input inside the app.find_multiply funcition in my python file, so i can perform operation there.

1

There are 1 answers

0
inclement On

Store references to your text fields, and access them later to do what you want.

I would delete your on_parent, replace the GridLayout with your own class MyGridLayout(GridLayout):, and have that class do:

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.text_fields = [MdTextField(hint_text= 'sc', helper_text= 'hello', size_hint_x= None, width = 40))] for _ in range(16)]

Then later when you want to access them, you can iterate through e.g. entered_numbers = [int(field.text) for field in self.text_field].

Of course that's just a basic example, in reality you'll want error checking etc.