PysimpleGUI 在 window 中制作 gif 动画

PysimpleGUI animate a gif in window

我正在尝试在代码执行计算时在窗口播放加载 gif。

我读过这个 post : 但它不适用于我的情况,因为我没有尝试使用 sg.PopupAnimated

我想做的是通过动画 gif 更新显示的窗口,直到代码结束并且 windows 关闭。

我确定它与循环有关,但我不知道是什么。

这是我的代码:

layout4 = [[sg.Image(r'C:\Users\...\Pictures\img.png'), sg.Text('Please wait')],
          [sg.Image(r'C:\Users\...\Pictures\animation.gif', size = (120,120), key = "Prog_bar")]]

window4 = sg.Window("HAL 220").Layout(layout4)
event, values = window4.Read()

while True:

    window4.FindElement("Prog_bar").UpdateAnimation("animation.gif",time_between_frames=100)

    if event is None :
        break

# ===== That would be the section of the code with the function and the loop =====

def main(alpha, alg_type):

    """ Set the variables for the loop """
    futures = []
    e = ProcessPoolExecutor(8)  # Processes is almost 8 times faster in this cases than ThreadPoolExecutor

    if alg_type == "O":
        F = FFR_ME
    elif alg_type == "NO":
        F = FFR_ME_NO
    else :
        raise TypeError("Algortithm type selected does not exist. Must be 'O' or 'NO'")

    """ Loop over the different task summarized in the tab 'N_tab' during the MPO_PROC step. """
    for task in N_tab["TASK_NUMBER"]:

         """ Declare variables N, n , f based on the previous calculations """ 

         N = int(N_tab.loc[N_tab["TASK_NUMBER"] == task, "N_Task"])
         n = int(N_tab.loc[N_tab["TASK_NUMBER"] == task, "n_i"])
         f = int(N_tab.loc[N_tab["TASK_NUMBER"] == task, "F"])

         """" Implement the function using the concurrent.future module for multiprocessing. """ 

         future = e.submit(F, N, n, f, alpha)     
         futures.append(future)   

    results = [ff.result() for ff in futures] 

    for i in range(len(results)):
        f = int(N_tab.loc[i, "F"])                                                
        N_tab.loc[i,"LBound"] = results[i][0][f]
        N_tab.loc[i,"UBound"] = results[i][1][f]
        N_tab.loc[i,"ME"] = (N_tab.loc[i,"UBound"] - N_tab.loc[i,"LBound"])/\
                                               (2*N_tab.loc[i,"N_Task"])
        N_tab.loc[i,"FFR"] = (N_tab.loc[i,"LBound"] + (N_tab.loc[i,"UBound"] - N_tab.loc[i,"LBound"])/2)/\
                                                N_tab.loc[i,"N_Task"]                                                

if __name__ == "__main__":
    main(alpha, alg_type)

# ====== End of the section end of the calculation


window4.close()

谢谢

您似乎在进行阻塞 read()

event, values = window4.Read() 应该在 while 循环内。但是,它仍然会阻塞。要使 read() 成为非阻塞函数,请使用 kwarg timeout.

while True:
   event, values = window4.Read(timeout=100) # every 100ms, fire the event sg.TIMEOUT_KEY
   window4.FindElement("Prog_bar").UpdateAnimation("animation.gif",time_between_frames=100)

    if event is None :
        break