Search code examples
pythonkivykivy-language

I can't read the value of text input in Kivy


My problem is when I want to display the value given by the user in the Text input text field.

When I want to get the text value from the input I get an empty value and I cannot display anything in the label field .

Here is my code :

  • The kv file :

    • MainGridLayout:
      
      <MainGridLayout>:
      cols: 1
      rows: 2
      BoxLayout:
          orientation: 'vertical'
          TextInput:
              id: my_text_input
              text: root.text_input_1
              hint_text :'Operations'
              multiline:False
              pos_hint: {'center_x': 0.5, 'center_y': 0.705}
              size_hint: 0.95, 0.5
              font_size: '30dp'
          Label:
              text: root.Display_text
              font_size: '45dp'
      StackLayout:
          Button:
              text:"+"
              spacing: .2, .2
              size: 85, 85
              size_hint: None, None
              on_press: root.on_button_plus()
      
      
  • The py file :

    • 
      from kivy.app import App
      from kivy.properties import StringProperty
      from kivy.uix.gridlayout import GridLayout
      
      
      # different layouts:
      
      class MainGridLayout(GridLayout):
      
           text_input_1 = StringProperty()
           Display_text = StringProperty()
      
           def on_button_plus(self):
               print(f'{self.text_input_1}')
      
      
           # run application:
      
           class CalculatorApp(App):
               pass
      
           CalculatorApp().run()
      

Solution

  • Actually, you don't need the variables text_input_1 and Display_text. To display the text from the my_text_input on the label you can add text: my_text_input.text instead of text: root.Display_text. Now, when the text of my_text_input changes, the label's text will change automatically and you don't need any functions. In on_button_plus() method you can do the same and print my_text_input's text using its text property (from all MainGridLayout's ids you take only my_text_input and print its text). The line text: root.text_input_1 unnecessary, too.

    Here is my editted version of your code:

    The .py file

    from kivy.app import App
    from kivy.properties import StringProperty
    from kivy.uix.gridlayout import GridLayout
    
    
    class MainGridLayout(GridLayout):
    
        def on_button_plus(self):
            print(f'{self.ids.my_text_input.text}')
    
    
    class CalculatorApp(App):
        pass
    
    
    CalculatorApp().run()
    

    The .kv file

    MainGridLayout:
    
    <MainGridLayout>:
        cols: 1
        rows: 2
        BoxLayout:
            orientation: 'vertical'
            TextInput:
                id: my_text_input
                hint_text :'Operations'
                multiline:False
                pos_hint: {'center_x': 0.5, 'center_y': 0.705}
                size_hint: 0.95, 0.5
                font_size: '30dp'
            Label:
                text: my_text_input.text  # change the text automatically
                font_size: '45dp'
        StackLayout:
            Button:
                text:"+"
                spacing: .2, .2
                size: 85, 85
                size_hint: None, None
                on_press: root.on_button_plus()