LuGibs
LuGibs

Reputation: 190

Make a function wait for a tkinter root.after() loop to finish before continuing executing

I have a call to a function playVideo() that then loops for frames of a video using tkinter root.after(5, playVideo). After I call playVideo I have some more code that handles a list which is populated in the playVideo. The problem is this code executes before the playVideo loop finishes.

Is there a way I can force the program to wait for the playVideo() to finish before continuing?

def myFunc():
    global myList

    # call to the looping function
    playVideo()

    # some code that handles the list

def playVideo():
    global myList

    ret, frame = currCapture.read()

    if not ret:
        currCapture.release()
        print("Video End")
    else:
        # some code that populates the list

        root.after(5, playVideo)

Upvotes: 0

Views: 435

Answers (1)

acw1668
acw1668

Reputation: 46687

You can try using wait_variable() function:

# create a tkinter variable
playing = BooleanVar()

Then using wait_variable() to wait for completion of playVideo():

def myFunc():
    global myList
    # call to the looping function
    playVideo()
    # some code that handles the list
    print('Handling list ...')
    # wait for completion of playVideo()
    root.wait_variable(playing)
    # then proceed
    print('Proceed ...')

def playVideo()
    global myList
    ret, frame = currCapture.read()
    if not ret:
        currCapture.release()
        print("Video End")
        playing.set(False) # update variable to make wait_variable() return
    else:
        # some code that populates the list
        root.after(5, playVideo)

Upvotes: 1

Related Questions