Resetting a Text Input (Kivy)

I have written an app which takes a barcode scan from a book, checks whether it is already in my book catalog[ue] and then flashes green or red depending on whether it is in or not.  I have a lot of books to catalog (about 1600) and about 90% of them are already done but I don’t know which 10%.  So I need to sort out that 10% and process them.   I scan the ISBN using a cuecat barcode reader into a Kivy textinput then check it against my list of ISBNs dumped from my existing database (maintained using Readerware).  I want to scan one book after another and separate them into two (ish) piles depending on whether it needs to be entered in the database. The Kivy textinput defaults to multiline.  In that mode you need to press a separate button to invoke a callback.  If multiline is set to False, each “enter” received will call a callback (on_text_validate).  So that means I can scan, check the book and scan again…

…or it would mean that if Kivy didn’t defocus the textinput widget whenever it calls on_text_validate.  After each can the widget needs to be refocussed. The widget can be refocussed by  setting it’s .focus attribute to True.  The trick however is to make sure that happens _after_ Kivy has defocussed the widget.  To do that you need to schedule a callback to occur after the next Kivy “frame”:

        Clock.schedule_once(self._refocus_text_input, 0)
        #from kivy.clock import Clock

    def _refocus_text_input(self,  arg1):

Awesome.  Now I don’t have to tap the widget to reset it after each scan.

2 Responses to Resetting a Text Input (Kivy)

  1. joranbeasley says:

    I think you can do this better with

    def on_focus(textInstance,isFocus):
    print self.scan_entry
    if not isFocus: self.scan_entry.Focus = True”’

    this should also trigger on enter if multiline is false

  2. I am not able to REFOCUS TextInput after reload of my screen

    When I reload the screen, I am able to clear TextInut, but not able to REFOCUS, help me to fix my following code?

    from import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen
    orientation: ‘vertical’

    id: Image_Input
    multiline: False
    focus: True
    on_text_validate: Image_Input.focus=True
    text: ‘RELOAD’
    on_press: root.reload(); Image_Input.text=”

    class MainScreen(Screen):
    def reload(self):
    print(“screen reloaded”)

    class TestApp(App):
    def build(self):
    # Create the screen manager
    screenManager = ScreenManager()
    return screenManager

    if __name__ == ‘__main__’:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: