简体   繁体   中英

How to call a function after Text has been entered in Kivy

I am trying to call a function after characters have been entered in my text input box. I tried bind but it calls it regardless if anything is entered. I am taking input from a hand scanner and displaying it in the textinput box. Then I will eventually send that including other info to a csv file.

main2.py

import kivy
import subprocess
import csv
import datetime
kivy.require('1.9.0')


from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.label import Label
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.core.window import Window





dt = datetime.date.today()
pafa = ''


#presentation = Builder.load_file('utest.kv')

class Barcode(TextInput):
    multiline = False

class PassFail(Label, Widget):
    pass

class UsbApp(App):



    def build(self):

        b= BoxLayout(orientation='vertical')
        t= Barcode(font_size = 50,

                    text="",

                    height=200,

                    background_color = (1,1,1,1)
                    )


        l=Label(text="Hey!",
                font_size=150
                )
        pf = PassFail(text='Pass/Fail',
                    font_size= 50,

                    background_color= (1,0,0,1),
                    color = (1,0,0,1),
                    bold=True
                    )

        t.bind(text = l.setter('text'))

        prompt=Label(text='Plug in device')

        b.add_widget(prompt)        
        b.add_widget(t)
        b.add_widget(pf)


        t.bind(text=Clock.schedule_once(self.run_test, 2))
        return b

    def detect(self):
        #detect when device is plugged in
        pass
        #device is detected change label

    #def on_text(self, barcode, clock):

        #self.barcode = Barcode().text

        #Clock.schedule_once(self.run_test, 2)

    def change_label(self):
        if self.prompt.text is 'Plug in device':
            self.prompt.text = 'Device detected'
        elif self.prompt.text == 'Device detected':
            self.prompt.text = 'Scan device'
        elif self.prompt.text == 'Scan device':
            self.prompt.text = 'Plug in device'

    def run_test(self, proc):

        #subprocess call to run app
        looptest = subprocess.call('sudo ./h2hlooptest -v', shell=True)

        #catch exit code then change label to Pass or fail
        if looptest == 0:
            pf = PassFail().text = 'Pass'

        else:
            pafa = PassFail().text = 'Fail'

        self.csv_write(pafa)

    def csv_write(self, pafa):

        with open("results.csv", "a") as csv_file:

            writer = csv.writer(csv_file, delimiter=',')
            csvlist=[[dt, Barcode().text, pafa]]
            writer.writerows(csvlist)

if __name__=="__main__":
    UsbApp().run()
t.bind(text=Clock.schedule_once(self.run_test, 2))

This doesn't bind the scheduling function to the text property, it calls the schedule_once function.

You need to pass a function to the bind method. In this case you could use a lambda function:

t.bind(text=lambda instance, value: Clock.schedule_once(self.run_test, 2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM