Search code examples
pythonkivykivy-language

Why Kivy is not refreshing/updating my screen when I need it?


I am having this problem:

A screen was created to show the messages that comes during an synchronization event that may last one minute, for example. More or less every two seconds one message will come.

I was expexting that when a message was received that message would be immediately printed.

But what happens with this code is that all the messages are printed at once when the whole syncronizarion ends, and not when each message comes, as expected.

.py:

class SyncScreen(Screen):
    content = StringProperty()

    def on_enter(self):
        self.content = "Synchronization messages"
        controller.synchronize(self.update_text)

    def update_text(self, msg):    # Callback
        self.content = self.content + msg

.kv:

<SyncScreen>:
    MDBoxLayout:
        orientation: "vertical"
        MDToolbar:
            title: "Synchronization"
        MDBoxLayout:
            orientation: "vertical"
            padding: 10
            TextInput:
                text: root.content
                size_hint: 1.0, 1.0
                multiline: True

Solution

  • Your synchronize function is blocking, Kivy can't draw anything until it returns. Run it in a thread, or do something like have it return (and get re-scheduled) every frame.