2

Why the textinput value isn't updated with this code? I need to capture the value typed by the user when I click on the button, but no matter what it seems that I can only get the default text.

This is a runnable example on python 3.4.3:

# -*- coding: utf-8 -*-
import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput 
from functools import partial 

class TestApp(App):
    def check(self, texto, *args):
        print(texto)

    def build(self):
        layout = FloatLayout()
        txtinput = TextInput(background_color=(1,1,1,1),
                                   font_size=16,
                                   size_hint=(None, None),
                                   size=(200, 35),
                                   pos_hint={"center_x":0.5, "bottom":0.8},
                                   multiline=False,
                                   text="write here...",
                                   use_bubble=True)
        boton = Button(size_hint=(None, None),
                              size=(150, 50),
                              pos_hint={"left":1, "bottom":1},
                              text="Check",
                              on_release=partial(self.check, txtinput.text))
        layout.add_widget(txtinput)
        layout.add_widget(boton)
        return layout

if __name__ == "__main__":
    TestApp().run() 

I'd like a pure Python answer, I don't really like to use kv files.

Saelyth
  • 1,694
  • 2
  • 25
  • 42

1 Answers1

0

Bind check method with with TextInput.bind() to your txtinput after creation txtinput :

txtinput = TextInput(background_color=(1,1,1,1), ...
txtinput.bind(text=self.check)

And input value became available in code snippet provided in question.

Currently check method is instance method and i suppose it always accept instance of App as first attribute, txtinput as second and input text as third.

To access your text in check method signature shall be updated to that one:

def check(self, txt_input, texto, *args):
    print(texto)
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • not really, it should print "this is not working" when I click on the button, but instead is printing an object when I type on the txtinput. Screenshot: http://prntscr.com/aezy94 – Saelyth Mar 14 '16 at 06:25
  • @Saelyth ohh that's related to param passing, see updated answer. – Andriy Ivaneyko Mar 14 '16 at 06:29
  • Also i've updated `txtinput.bind(text=check)` to `txtinput.bind(text=self.check)` – Andriy Ivaneyko Mar 14 '16 at 06:31
  • Now it works, even though I don't understand how. I'll do some tests to figure it out. Thank you :D – Saelyth Mar 14 '16 at 06:53
  • 1
    You are welcome :) You can take a look on that SO Question to extend python knowledge: http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self-in-python – Andriy Ivaneyko Mar 14 '16 at 06:56