MagTun
MagTun

Reputation: 6185

Kivy: how to display a widget while waiting for another one to be displayed (both called from a same event)

When an "OK button" is clicking, my kivy app retrieves a list of sometimes 100+ folders and displays a GridLayout with 4 columns and 1 row per folder. Each row has 3 scrollable labels and 1 checkbox. This GridLayout sometimes takes close to 12 sec to be generated so I would like to display something (a label, an image...) in the meantime.

Attempt 1: My "Ok button" calls a def DisplayTable. I tried to simply add self.add_widget(Label_when_waiting) right at the beginning of DisplayTable (so before any processing or generating the GridLayout) but the Label_when_waiting is displayed only when GridLayout is displayed.

Attempt 2: I tried to separate def DisplayTable into two def, Diplay_Label_when_waiting(the one called by the "OK button") and DisplayTable:

def Diplay_Label_when_waiting(self, *args):
    self.add_widget(Label_when_waiting)
    DisplayTable(self, *args)

But here again, Label_when_waiting is displayed only when GridLayout is displayed.

So how can I display Label_when_waiting before GridLayout knowing that both displays have to be triggered by the "Ok button"

Upvotes: 2

Views: 1397

Answers (1)

Yoav Glazner
Yoav Glazner

Reputation: 8066

Use Clock.schedule_once to display the Grid after the label is shown:

def Diplay_Label_when_waiting(self, *args):
    self.add_widget(Label_when_waiting)
    Clock.schedule_once(lambda dt: DisplayTable(self, *args), 0)

You can also use delayable from kivyoav (DISCLAIMER - I'm the author ...)

from kivyoav.delayed import delayable

@delayable
def Diplay_Label_when_waiting(self, *args):
    self.add_widget(Label_when_waiting)
    yield 0.0 # delay of 0ms , will cause the UI to update...
    DisplayTable(self, *args)

Upvotes: 4

Related Questions