Updating display during the running of a method

18 views
Skip to first unread message

Tim Woodhams

unread,
Jan 23, 2023, 6:19:11 PM1/23/23
to Kivy users support
Hi, I am trying to teach myself Kivy (and python) and have come to a problem. I have a method (see below) based around music and dance that I want to change the display based on time. Currently the display doesn't update during the running of the method but does when it is finished. I've search the net but I am very confused about how to update the display whenever I would like to.

Thanks for your help

Method:
    def playRoutine(self,routine):
        moves = routine[0] # reads in list of moves
        total = 0
        moveBeats = []
        for b in routine[1]: # constructs a list of beat to switch move
            total += b
            moveBeats.append(total)
        beats = 0
        move = 0
        secPerBeat = 60/routine[2] #convert bpm to seconds per beat
        startTime = time.time()
        while beats < moveBeats[-1]:
            try: #deals with end of list
                self.ids.current.text = moves[move] #changes display
                self.ids.next.text = moves[move+1]
            except:
                self.ids.current.text = moves[move]
                self.ids.next.text = ""
            while beats < moveBeats[move]:
                beats += 1
                #sleep for the correct time for next beat
                time.sleep(startTime+beats*secPerBeat-time.time())
                self.flash()
            move += 1
               
    def flash(self):
        '''Temp: displays word flash every beat.
        to do: switch to flash background and
        add optional tick'''
        self.ids.flasher.text = "flash"
        time.sleep (0.05)
        self.ids.flasher.text = ""

Robert

unread,
Jan 24, 2023, 4:36:39 PM1/24/23
to Kivy users support
More general that Kivy.

UI apps are event driven, so UI changes are scheduled on an event loop.
Events are scheduled based on the current event, input events, clock events, or thread events.

NEVER use sleep() in the main thread, this will block the event loop and stall the app UI (similarly never join() a thread).

Perhaps this will get you started https://kivy.org/doc/stable/api-kivy.clock.html

Tim Woodhams

unread,
Jan 24, 2023, 7:11:05 PM1/24/23
to Kivy users support
Thanks Robert

Yes Clock.schedule_once does exactly what I need. Thank you for the general advice too.

Cheers
Tim

Reply all
Reply to author
Forward
0 new messages