使函数等待tkinter root.after()循环完成后再继续执行

时间:2020-02-25 12:20:40

标签: python asynchronous tkinter

我调用了函数playVideo(),然后使用tkinter root.after(5, playVideo)循环播放视频帧。调用playVideo之后,我还有更多代码可以处理在playVideo中填充的列表。问题在于该代码在playVideo循环完成之前执行。

有没有一种方法可以迫使程序在继续播放之前等待playVideo()完成?

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)

1 个答案:

答案 0 :(得分:1)

您可以尝试使用wait_variable()函数:

# create a tkinter variable
playing = BooleanVar()

然后使用wait_variable()等待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)