mri3
mri3

Reputation: 256

Text from TextInput not being updated

I am attempting to use kv language to redo a program I have already done. The first was done in python. Now when I use kivy language I cannot get anything from my UI. In this example. when the Barcode text changes it should update it's text property which should then fire off my function. Problem is it is not recognizing the change in the text input box.

main6.py

class Barcode(TextInput):

    l_prompt= ObjectProperty()
    btext = StringProperty()

    # capture barcode
    def onText(self):
        print ('in onText')
        c=True
        while c:
            print('in loop')
            time.sleep(1/10)
            print(self.text)
            if len(self.text) <=5:
                c= True
            else:
                c = False
                print('in else')
                self.l_prompt.change_label('test')
                Clock.schedule_once(self.parent.run_test,1)

class MainLayout(FloatLayout):

    dt = str(datetime.today()) 
    pafa = ''       #pass/fail
    barcode = ''    #barcode number
    counter = 0 #item counter

    prompt_wid = ObjectProperty()
    barcode_wid = ObjectProperty(rebind = True)
    passfail_wid = ObjectProperty()
    counterlabel_wid = ObjectProperty()


    def __init__(self, **kwargs):
        super(MainLayout, self).__init__(**kwargs)

        #start test
        #self.starttest(self)

        self.l=Label()
        #self.barcode_wid.bind(text=self.l.setter('text'))

    def detect(self, d):
        #detect when device is plugged in
        t = True
        while t:
            time.sleep(1)
            dev = usb.core.find(idVendor= 0x2996)

            if dev is None:


                t=False

                self.prompt_wid.change_label('scan')  #label change
                #self.barcode_wid.focus=True #brings cursor up for input scan


                j = threading.Thread(target=self.barcode_wid.onText)
                j.daemon = True
                j.start()

ex6.kv

#: kivy 1.9


<MainLayout>:
    orientation: 'vertical'
    size: root.size

    prompt_wid: prompt
    barcode_wid: barcode
    passfail_wid: passfail
    counterlabel_wid: counterlabel

    canvas.before: 
        Color: 
            rgb: 0, 0, .6 
        Rectangle: 
            pos: self.pos 
            size: self.size 

    Prompt:
        id: prompt

        canvas.before: 
            Color: 
                rgb: .6, .6, .6 
            Rectangle: 
                pos: self.pos 
                size: self.size 
    Barcode:
        id: barcode
        l_prompt: prompt
        focus: 'True'
        text:'hey'

<Barcode>:

    pos_hint: {'center_x': .5, 'top': 0.666 }
    size_hint:(1,.3)
    font_size: self.height/3
    text: ''
    multiline: 'False'
    padding: 0,(self.height/3)

presentation = Builder.load_file('ex6.kv')


class MainApp(App):
    def build(self):
        ml = MainLayout()
        Clock.schedule_interval(ml.detect, 2)
        return MainLayout()


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

I am attempting to use the Kivy Properties but I think I a missing a fundamental link between the py and kv file because none of my Labels (l_prompt) are updating as well. Everything runs behind the scene ok but no UI change except when enter characters into the Text Input box.

Upvotes: 1

Views: 335

Answers (2)

mri3
mri3

Reputation: 256

I found i had to start my test from the init method of my app. So i commented out the code in my build section and uncommented out the code in my init method "self.starttest". I had changed it thinking calling from the build method looked better but I think what happened is that because it skipped the init i basically disconnected the functionality from the UI.

main6.py

class Barcode(TextInput):

    l_prompt= ObjectProperty()
    btext = StringProperty()

    # capture barcode
    def onText(self):
        print ('in onText')
        c=True
        while c:
            print('in loop')
            time.sleep(1/10)
            print(self.text)
            if len(self.text) <=5:
                c= True
            else:
                c = False
                print('in else')
                self.l_prompt.change_label('test')
                Clock.schedule_once(self.parent.run_test,1)

class MainLayout(FloatLayout):

    dt = str(datetime.today()) 
    pafa = ''       #pass/fail
    barcode = ''    #barcode number
    counter = 0 #item counter

    prompt_wid = ObjectProperty()
    barcode_wid = ObjectProperty(rebind = True)
    passfail_wid = ObjectProperty()
    counterlabel_wid = ObjectProperty()


    def __init__(self, **kwargs):
        super(MainLayout, self).__init__(**kwargs)

        #start test
        self.starttest(self)

        self.l=Label()
        #self.barcode_wid.bind(text=self.l.setter('text'))

    def detect(self, d):
        #detect when device is plugged in
        t = True
        while t:
            time.sleep(1)
            dev = usb.core.find(idVendor= 0x2996)

            if dev is None:


                t=False

                self.prompt_wid.change_label('scan')  #label change
                #self.barcode_wid.focus=True #brings cursor up for input scan


                j = threading.Thread(target=self.barcode_wid.onText)
                j.daemon = True
                j.start()

ex6.kv

#: kivy 1.9


<MainLayout>:
    orientation: 'vertical'
    size: root.size

    prompt_wid: prompt
    barcode_wid: barcode
    passfail_wid: passfail
    counterlabel_wid: counterlabel

    canvas.before: 
        Color: 
            rgb: 0, 0, .6 
        Rectangle: 
            pos: self.pos 
            size: self.size 

    Prompt:
        id: prompt

        canvas.before: 
            Color: 
                rgb: .6, .6, .6 
            Rectangle: 
                pos: self.pos 
                size: self.size 
    Barcode:
        id: barcode
        l_prompt: prompt
        focus: 'True'
        text:'hey'

<Barcode>:

    pos_hint: {'center_x': .5, 'top': 0.666 }
    size_hint:(1,.3)
    font_size: self.height/3
    text: ''
    multiline: 'False'
    padding: 0,(self.height/3)

presentation = Builder.load_file('ex6.kv')


class MainApp(App):
    def build(self):
        #ml = MainLayout()
        #Clock.schedule_interval(ml.detect, 2)
        return MainLayout()


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

)

Upvotes: 0

inclement
inclement

Reputation: 29450

At no point does your program call your onText function. Did you mean to name it on_text, which would automatically be called when the text property changes?

Upvotes: 1

Related Questions