Resetting a Text Input (Kivy)
August 10, 2014 2 Comments
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): self.scan_entry.focus=True
Awesome. Now I don’t have to tap the widget to reset it after each scan.