Kivy: label text is not updated during loop

I have a problem when I try to update label text during a for loop. There are similar entries (for example: Updating the properties of the kivy widget when the code runs ), but they do not seem to correspond to my problem exactly (or I missed the point ...). I run the following code:

.

* R:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
#from time import sleep

class MyBox(BoxLayout):
    tobeupd = StringProperty()

    def __init__(self,*args,**kwargs):
        super(MyBox,self).__init__(*args,**kwargs)
        self.tobeupd = '#'

    def upd_ltxt(self):
        for i in range(1,10):
            self.tobeupd = str(i)
            print(self.tobeupd)
            input('Write something: ')  # new line, see edit below
            #sleep(0.5) 

class updApp(App):
    def build(self):
        return MyBox()

if __name__ == '__main__':
    updApp().run() 

*. Kilovolt

<MyBox>:
    orientation: 'horizontal'
    cols: 2
    Label:
        text: root.tobeupd
    Button:
        text: 'Start Update'
        on_release: root.upd_ltxt()

While the β€œprint statement updates the shell regularly, the label text is only updated at the end of the for-loop. Can someone explain to me why Qiwi works this way and how can I overcome this problem?

EDIT: According to PM2Ring and Gugas, I changed the code to avoid the sleep function. The problem remains if I ask the user to enter something before the cycle continues. Values ​​are updated in the shell, but not on the shortcut.

+4
2

threading .
kivy, , . threading .
threading, , .
:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.lang import Builder
import threading

Builder.load_string('''

<MyBox>:
    orientation: 'horizontal'
    cols: 2
    Label:
        text: root.tobeupd
    Button:
        text: 'Start Update'
        on_release: root.upd_ltxt()

''')

class MyBox(BoxLayout):
    tobeupd = StringProperty()

    def __init__(self,*args,**kwargs):
        super(MyBox,self).__init__(*args,**kwargs)
        self.tobeupd = '#'

    def upd_ltxt(self):
        threading.Thread(target=self.update_label).start()

    def update_label(self):
        for i in range(1,10):
            print(self.tobeupd)
            self.tobeupd = str(i)
            input('Write something: ')  # new line, see edit below



class updApp(App):
    def build(self):
        return MyBox()

if __name__ == '__main__':
    updApp().run()

, , . .
, .

id kv:

Button:
    id: updatebutton
    text: 'Start Update'
    on_release: root.upd_ltxt()

:

def update_label(self):

    self.ids.updatebutton.disabled = True

    for i in range(1,10):
        self.tobeupd = str(i)
        input('Write something: ')

    self.ids.updatebutton.disabled = False
+2

Kivys clock Class, . , . , .

from kivy.clock import Clock   

def to_be_called_back(self,dt):
    print("This function should be periodically executed")

def do_the_loop(self):
    Clock.schedule_interval(self.to_be_called(),0.5)

to_be_called() 0,5 . dt , -, Clock ( )

- do_the_loop() . , . Class Head .

0

Source: https://habr.com/ru/post/1671119/


All Articles