pythonkivykivy-language

Timing issue with Kivy


I'm some what new to Kivy / KivyMD and struggling with a time issue of some sort.

I have a Kivy ScreenManager enabled with 2 screens, a splash which essentially works and a main screen I want to advance to when a custom function completes. The custom function is doing some validation on a SQLite database and it updates a status property for the interface.

The custom function is called when the splash screen display. While in that function I want a Label on the splash screen to update showing progress. What's happening after several attempts is the data routine is not updating the label until it's finished. I've tried different events on_start, on_enter and init but the effect remains the same.

main.py

class Interface(ScreenManager):
  status = StringProperty("Data Routines")
  stat_clock = None
  
  def __init__(self, **kwargs)
    super().__init__(**kwargs)

    self.stat_clock = Clock.schedule_interval(self.check_status, 3)
    self.data_routines()

  def check_status(self):
    if self.status == "Finished":
      self.stat_clock.cancel()
      self.current = "Main Screen"

  def data_routines(self):
    ... a bunch of code ...
    self.status = "whatever"
    ... a bunch of more code ...
    self.status = "Finished"

kivy.kv:

Interface:
  Screen:
    name: "Splash Screen"
    AnchorLayout:
      anchor_x: "center"
      anchor_y: "center"
      Image:
        source: "image.png"
    AnchorLayout:
      anchor_x: "center"
      anchor_y: "center"
      Label:
        text: root.status
        padding: [0, dp(260), 0, 0]
  Screen:
    name: "Main Screen"
    AnchorLayout:
      anchor_x: "center"
      anchor_y: "center"
      Label:
        text: "Main Screen"
        size_hint: None, None
        height: dp(40)
        width: dp(80)

Any suggestions greatly appreciated.


Solution

  • You are running the data_routines() on the main thread. The GUI is only updated on the main thread, so it must wait until you release the main thread (return from data_routines()) before it updates.

    A fix is to run the data_routines() in another thread and call Clock.schedule_once() from that thread to initiate any methods that update the GUI. For example, updating the status property. And those GUI updating methods should be short and quick.