Update properties of a kivy widget while running code

倾然丶 夕夏残阳落幕 提交于 2019-12-07 10:03:54

问题


I want to update the properties of a kivy widget while running something...

Example:

class app(App):
    def build(self):
        self.layout = Layout()
        self.name = Label(text = "john")
        self.layout.add_widget(self.name)
        return self.layout

    def update(self):
        for i in range(50): #keep showing the update
            self.name.text = str(i)
            #maybe some sleep here

obj = app()
obj.run()
obj.update()

This is gonna show me only the final result of the loop. I'd like to keep updating the label.text while the loop goes.

I looked for something like the bind(), setter() and ask_update() functions, but if are these funcs, I didn't get how to use them.

------------------ EDIT -----------------------

Trying to adapt to inclement answer (running the update function in other thread using Clock), I got the code below trying to follow the real idea of my problem, but still not working:

class main():
    def __init__(self, app):
        self.app = app

    ... some code goes here ...

    def func(self):
        Clock.schedule_once(partial(self.app.update, self.arg_1, self.arg_2), 0)

class app(App):
    def build(self):
            self.main = main(self)
            self.layout = Layout()
            self.name = Label(text = "john")
            self.layout.add_widget(self.name)
            return self.layout

    ... some code goes here ...

    def update(self, dt, arg_1, arg_2):
        self.name = arg_1
        sleep(5)
        self.name = arg_2

obj = app()
obj.run()

I need to call the funcfunction and make it update the label text exactly when I order the text change in update function.


回答1:


You need to avoid blocking the main thread. In most cases, it's convenient to just use kivy's clock. You can do something like the following.

from kivy.clock import Clock

class app(App):
    def build(self):
        self.layout = Layout()
        self.name = Label(text = "john")
        self.layout.add_widget(self.name)
        self.current_i = 0
        Clock.schedule_interval(self.update, 1)
        return self.layout

    def update(self, *args):
        self.name.text = str(self.current_i)
        self.current_i += 1
        if self.current_i >= 50:
            Clock.unschedule(self.update)



回答2:


Not sure if anyone still needs the answer, but it seems that your mistake is here:

def update(self, dt, arg_1, arg_2):
    self.name = arg_1
    sleep(5)
    self.name = arg_2

You either assign arg1 and arg2, presumably strings, to the widget, which doesn't seem to make much sense. Or creating a new widget and reassigning it to the same variable, which is also not correct. Instead you can assign to property directly:

def update(self, dt, arg_1, arg_2):
    self.name.text = arg_1
    sleep(5)
    self.name.text = arg_2

Or you can create a StringProperty on your main object and bind self.name.text to it, but I personally prefer updating the screen manually. Explicit is better than implicit and all that.



来源:https://stackoverflow.com/questions/27213545/update-properties-of-a-kivy-widget-while-running-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!