Search code examples
pythonkivykivy-language

Read the text from Textinput dynamically when text changed in text box?


I'm new to kivy , I'm trying to write an application using kivy in python, I got struck at one point where i have to read text from textinput whenever it is changed and based on that i want to implement my button's functionality - I have gone through all the documentation but i couldn't figure out how to do it - Can any body tell me how can I resolve this or am I missing something?

from __future__ import print_function
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.core.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import *
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
import walascan
from kivy.clock import Clock
import os


kv = """
<KartScan>:
    IntroScreen:

<IntroScreen@Screen>:
    orientation: 'horizontal'
    name: 'introscreen'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'index.png'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        BoxLayout:
            orientation:'horizontal'
            size_hint: .5, .1
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            spacing: 20
            pos_hint: {'center_x':.8, 'center_y': .8}
            AnchorLayout:
                anchor_x: 'left'
                size_hint_x: .5
                TextInput:
                    id: waybill
                    width: 20
                    text: "Enter Waybill No."
                    multiline: False
                    height: self.minimum_height
                    size_hint_y: None
                    font_size: 30
                    focus: True
                    on_text_validate: app.on_waybill()
            AnchorLayout:
                anchor_x: 'right'
                size_hint_x: None
                Button:
                    size_hint: None, None
                    height: 50
                    width: self.texture_size[0]
                    padding: 10, 10
                    text: "Add"
                    on_press:app.buttonClicked()
                    on_release: root.current = 'mainpage'
            AnchorLayout:
                anchor_x: 'right'
                size_hint_x: None
                Button:
                    size_hint: None, None
                    height: 50
                    width: self.texture_size[0]
                    padding: 10, 10
                    text: "Compare"
                    on_press:app.buttonClicked()
                    on_release: root.current = 'mainpage'
"""

Builder.load_string(kv)

waybill = TextInput(text="Enter Waybill No.", multiline=False)



class KartScan(FloatLayout):
    def __init__(self, **kwargs):
        super(KartScan, self).__init__(**kwargs)
        self.register_event_type('on_text_validate')

    def on_text(self, *args):
        print('new value is ', waybill.text)

    def on_text_validate(self):
        pass

    def on_focus(self, obj, focused):
        if not focused:
            self.dispatch('on_text_validate')


class KartScanApp(App):

    def build(self):
        return KartScan()

    def buttonClicked(self):
        popup = Popup(title='Result',
                      content=Label(text=self.on_waybill()),
                      size_hint=(None, None), size=(100, 100))
        popup.bind()
        popup.open()

    def getwlbtstate(self):
        return walascan.mainAntennas()

    def on_waybill(self):
        waybill.bind(text=KartScan.on_text_validate)
        # popup = Popup(title='Result',
        #               content=Label(text=waybill.text),
        #               size_hint=(None, None), size=(100, 100))
        # popup.bind()
        # popup.open()
        return waybill.text

if __name__ == '__main__':
    KartScanApp().run()

Solution

  • kv file

    TextInput:
        on_text: print(self.text)
    

    You could use on_text to call a function when the text changes. I think it sometimes even triggers when no change has occured.