Search code examples
pythonkivykivy-languagekivymd

How to Assign TextInput(Factory object) value to variable


I've been trying to assign the text input to a variable my TextInput object is inside a factory object (object_1)

I've tried to Assign it using


txt_1=self.root.ids.txt.text

but it dosent work

Here is the whole code

from kivymd.app import App
from kivy.lang import Builder
from kivy.factory import Factory 

kv='''
#:import Factory kivy.factory.Factory

# this is object 1 which will be added to main grid

<object_1@BoxLayout>:

    orientation:'vertical'
    size_hint_y:None
    adaptive_height: True
    height:self.minimum_height 
    id:obj_1

    TextInput:
        size_hint:None,None 
        id:txt



#main grid

BoxLayout:
    orientation:'vertical'
    size_hint_y:None
    adaptive_height: True
    height:self.minimum_height 

    GridLayout:
        cols:1
        size_hint_y:None 
        adaptive_height:True
        height:self.minimum_height 
        id:sc_grid          
        Button:
            size_hint:None,None 
            text: 'Add 1'
            on_press:
                app.add_1()          
                    
        Button:
            size_hint:None,None 
            text: 'Assign value'
            on_press:
                app.Assign()                
                
'''

class MyApp(App):
    
    def build(self):
        return Builder.load_string(kv)

    
    def add_1(self):
        self.root.ids.sc_grid.add_widget(Factory.object_1())

    # this crashes

    def Assign(self):
        txt_1=self.root.ids.txt.text
        
MyApp().run()


In the above code object_1 in added to main grid when button is pressed

On press TextInput object appears in main grid

After entering the value,i want to click the assign button to assign value


Solution

  • I've figured out an alternative solution to solve this by assigning it though the kv, Here is the code

    from kivymd.app import App
    from kivy.lang import Builder
    from kivy.factory import Factory 
    
    kv='''
    #:import Factory kivy.factory.Factory
    
    # this is object 1 which will be added to main grid
    
    <object_1@BoxLayout>:
    
        orientation:'vertical'
        size_hint_y:None
        adaptive_height: True
        height:self.minimum_height 
        id:obj_1
    
        TextInput:
            size_hint:None,None 
            id:txt
                            
        Button:
            size_hint:None,None 
            text: 'Assign value'
            on_press:
                txt_1=root.ids.txt.text
    
    #main grid
    
    Screen:
        id:scrn
        BoxLayout:
            orientation:'vertical'
            size_hint_y:None
            adaptive_height: True
            height:self.minimum_height 
    
            GridLayout:
                cols:1
                size_hint_y:None 
                adaptive_height:True
                height:self.minimum_height 
                id:sc_grid          
                Button:
                    size_hint:None,None 
                    text: 'Add 1'
                    on_press:
                        app.add_1()          
    
    '''
    
    class MyApp(App):
        
        def build(self):
            return Builder.load_string(kv)
    
        
        def add_1(self):
            self.root.ids.sc_grid.add_widget(Factory.object_1())
    
    MyApp().run()
    

    But i still hope someone has a solution to do this via the App class