Search code examples
pythonwindowskivykivymd

Why doesn't my Kivy Text input work in returning the value to root when I use kivy.clock


import kivy
from kivy.lang.builder import Builder
from kivy.app import App
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.widget import Widget
from kivy.clock import Clock
Builder.load_string('''
<apper>
    input_val : input_t
    BoxLayout:
        size: root.size
        Label:
            id: label_t
            text: root.texter
        TextInput:
            id: input_t
        Button:
            on_press : root.doer()
''')
class apper(Widget):
    texter = StringProperty()
    input_val = ObjectProperty(None)
    run = 0
    def doer(self, dt):
        self.run+=1 # keep track of number of times this function ran
        #self.texter = self.ids.input_t.text
        print(self.ids.input_t.text, self.run) #prints an empty string for self.ids.input_t.text
        self.texter = self.input_val.text 
class runs(App):
    def build(self):
        Clock.schedule_interval(apper().doer, 1.0/2.0)
        return apper()
runs().run()

#the code works when you remove the Clock statement and the second argument or apper.doer() and you have to press the button
#to rename the label

#how comes it doesnt work when you introduce the clock statement

#Note that I am new to kivy but not new to programming

When I use a button to run the function on press the correct values from text input are returned but using a clock it doesn't work, can anyone help. Also when the program should be running properly with the Clock then the button is unnecessary since it updates the label with the text of the TextInput every 0.5 seconds. Is there any way the code can be rewritten or mended such that the TextInput returns a value and not an empty string.


Solution

  • move the clock to ur apper class: and put it like this

    Clock.schedule_interval(self.doer, 1/2)
    

    and declare doer like this:

    def doer(self, *args):
    

    and feel free to check this tuto i uploaded recently https://www.youtube.com/watch?v=DbX-CdVob6E&t=57s