更新 Popup.Animated 以播放 gif 直到外部任务完成 (PYSimpleGUI)

2024-01-02

我希望创建一个 UI,在执行另一项任务时显示动画弹出窗口。完成后将退出。我正在使用 PYSimpleGUI 并使用列出的示例here https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms%20old/Demo_Threaded_Work.py为我的工作奠定基础。一旦我启动代码并在任务完成后退出,我就可以显示动画的单帧,但无法让它播放整个 gif。代码:

    import queue
    import threading
    import time
    import PySimpleGUI as sg

    # ############################# User callable CPU intensive code #############################
    # Put your long running code inside this "wrapper"
    # NEVER make calls to PySimpleGUI from this thread (or any thread)!
    # Create one of these functions for EVERY long-running call you want to make

    def long_function_wrapper(work_id, gui_queue):
      # LOCATION 1
      # this is our "long running function call"
      #time.sleep(10)  # sleep for a while as a simulation of a long-running computation

     x = 0
     while True:
         print(x)
         time.sleep(0.5)
         x = x + 1
         if x == 5: 
            break



    # at the end of the work, before exiting, send a message back to the GUI indicating end
    gui_queue.put('{} ::: done'.format(work_id))
    # at this point, the thread exits
    return

    def the_gui():
    gui_queue = queue.Queue()  # queue used to communicate between the gui and long-running code

    layout = [[sg.Text('Multithreaded Work Example')],
              [sg.Text('This is a Test.', size=(25, 1), key='_OUTPUT_')],
              [sg.Button('Go'), sg.Button('Exit')], ]



    window = sg.Window('Multithreaded Window').Layout(layout)


    # --------------------- EVENT LOOP ---------------------
    work_id = 0
    while True:
        event, values = window.Read(timeout=100)  # wait for up to 100 ms for a GUI event

        if event is None or event == 'Exit':
            #sg.PopupAnimated(None)
            break
        if event == 'Go':           # clicking "Go" starts a long running work item by starting thread
            window.Element('_OUTPUT_').Update('Starting long work %s'%work_id)


            # LOCATION 2
            # STARTING long run by starting a thread
            thread_id = threading.Thread(target=long_function_wrapper, args=(work_id, gui_queue,), daemon=True)
            thread_id.start()
            #for i in range(200000):

            work_id = work_id+1 if work_id < 19 else 0

            #while True:
            sg.PopupAnimated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', time_between_frames=100)    
                #if message == None:
                    #break   
        # --------------- Read next message coming in from threads ---------------
        try:
            message = gui_queue.get_nowait()    # see if something has been posted to Queue

        except queue.Empty:                     # get_nowait() will get exception when Queue is empty
            message = None                      # nothing in queue so do nothing


        # if message received from queue, then some work was completed
        if message is not None:
            # LOCATION 3
            # this is the place you would execute code at ENDING of long running task
            # You can check the completed_work_id variable to see exactly which long-running function completed
            completed_work_id = int(message[:message.index(' :::')])
            sg.PopupAnimated(None)


        #window['_GIF_'].update_animation(sg.DEFAULT_BASE64_LOADING_GIF, time_between_frames=100)
        #window.read(timeout = 1000)

    # if user exits the window, then close the window and exit the GUI func
    window.Close()

    ############################# Main #############################

    if __name__ == '__main__':
        the_gui()
        print('Exiting Program'

)

您在仅执行一次的“if”语句内调用了 popup_animated。

您必须为要显示的每一帧调用 popup_animated。它不会作为在后台运行的任务而分离出来。

只要有后台任务运行,对代码的这种更改就会使动画保持持续状态。

import queue
import threading
import time
import PySimpleGUI as sg


# ############################# User callable CPU intensive code #############################
# Put your long running code inside this "wrapper"
# NEVER make calls to PySimpleGUI from this thread (or any thread)!
# Create one of these functions for EVERY long-running call you want to make

def long_function_wrapper(work_id, gui_queue):
    # LOCATION 1
    # this is our "long running function call"
    # time.sleep(10)  # sleep for a while as a simulation of a long-running computation

    x = 0
    while True:
        print(x)
        time.sleep(0.5)
        x = x + 1
        if x == 5:
            break
    # at the end of the work, before exiting, send a message back to the GUI indicating end
    gui_queue.put('{} ::: done'.format(work_id))
    # at this point, the thread exits
    return


def the_gui():


    gui_queue = queue.Queue()  # queue used to communicate between the gui and long-running code

    layout = [[sg.Text('Multithreaded Work Example')],
              [sg.Text('This is a Test.', size=(25, 1), key='_OUTPUT_')],
              [sg.Text(size=(25, 1), key='_OUTPUT2_')],
              [sg.Button('Go'), sg.Button('Exit')], ]

    window = sg.Window('Multithreaded Window').Layout(layout)

    # --------------------- EVENT LOOP ---------------------
    work_id = 0
    while True:
        event, values = window.Read(timeout=100)  # wait for up to 100 ms for a GUI event

        if event is None or event == 'Exit':
            # sg.PopupAnimated(None)
            break
        if event == 'Go':  # clicking "Go" starts a long running work item by starting thread
            window.Element('_OUTPUT_').Update('Starting long work %s' % work_id)
            # LOCATION 2
            # STARTING long run by starting a thread
            thread_id = threading.Thread(target=long_function_wrapper, args=(work_id, gui_queue,), daemon=True)
            thread_id.start()
            # for i in range(200000):

            work_id = work_id + 1 if work_id < 19 else 0

            # while True:
            # if message == None:
            # break
        # --------------- Read next message coming in from threads ---------------
        try:
            message = gui_queue.get_nowait()  # see if something has been posted to Queue
        except queue.Empty:  # get_nowait() will get exception when Queue is empty
            message = None  # nothing in queue so do nothing
        # if message received from queue, then some work was completed
        if message is not None:
            # LOCATION 3
            # this is the place you would execute code at ENDING of long running task
            # You can check the completed_work_id variable to see exactly which long-running function completed
            completed_work_id = int(message[:message.index(' :::')])
            window.Element('_OUTPUT2_').Update('Finished long work %s' % completed_work_id)
            work_id -= 1
            if not work_id:
                sg.PopupAnimated(None)

        if work_id:
            sg.PopupAnimated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', time_between_frames=100)

        # window['_GIF_'].update_animation(sg.DEFAULT_BASE64_LOADING_GIF, time_between_frames=100)
        # window.read(timeout = 1000)

    # if user exits the window, then close the window and exit the GUI func
    window.Close()

############################# Main #############################

if __name__ == '__main__':
    the_gui()
    print('Exiting Program')

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新 Popup.Animated 以播放 gif 直到外部任务完成 (PYSimpleGUI) 的相关文章

随机推荐

  • 重新排序内存中的字节,然后写入文件

    我在内存中有一个数据块 从memory ptr 该程序的作用是对每个 qword 进行字节反转 然后将其写入文件 例如 18171615141312112827262524232221将写成1112131415161718212223242
  • 在 pandas 数据框中查找具有相同列值的行

    我有两个具有不同列大小的数据框 其中四个列在两个数据框中可以具有相同的值 我想在 df1 中创建一个新列 如果 df2 中的一行与 df1 中的一行具有相同的 A B C 和 D 列值 则该新列的值为 1 如果没有这样的行 我希望该值为 0
  • 如何为具有 VARBINARY(MAX) 字段的表生成 INSERT 脚本?

    我有一张桌子 上面有VARBINARY MAX 字段 SQL Server 2008 具有FILESTREAM 我的要求是 当我部署到生产环境时 我只能向 IT 团队提供一组按特定顺序执行的 SQL 脚本 我在生产中制作的新表有这个VARB
  • 未设置 vs. = NULL [重复]

    这个问题在这里已经有答案了 可能的重复 使用 PHP 释放内存哪个更好 unset 或 var null https stackoverflow com questions 584960 whats better at freeing me
  • ExtJS 图表的性能比 FusionCharts 更好吗?

    我们正在考虑在应用程序中用 ExtJS 图表替换 FusionCharts 因为 我们已经在整个 UI 中使用了 ExtJS 如果能够消除另一个商业第三方依赖项和 API 的开销和费用 那就太好了 我们希望能够在无 Flash 的移动设备上
  • 如何在 Ruby on Rails 中覆盖 Materialise CSS?

    我一直在互联网上浏览一些关于 Rails 中的 Materialise 的帖子 但是这个领域似乎非常模糊 我目前正在使用 Materialize sass gem 我没有找到很多有用的帖子 我决定来这里 这是我的页面代码pages disc
  • ggplot2 在箱线图顶部添加文本

    我有一个正在绘制的数据ggplot2作为箱线图 看起来像 gt head varf sID variable value 1 SP SA036 SA040 CM0001 0 492537313 2 SP SA036 SA040 CM0001
  • jQuery 加载动态元素

    我正在尝试对动态添加到页面上某些容器的元素进行一些条件操作 但我错过了一个事件 假设我有一个容器 div div 我可以轻松地将事件处理程序绑定到所有新元素的单击函数 使用 container on click sub element fu
  • Siri 无法在现有项目中运行

    我必须使用 Siri 通过我的应用程序发起 VoIP 呼叫 它在演示项目中工作 但当我将意图扩展添加到现有项目中时 Siri 不再工作 在系统设置中 我的应用程序未显示在应用程序支持部分中 Plist配置如下 另请参阅扩展的 plist 配
  • hashmap的负载因子和容量

    如何找到hashmap当前的负载因子和容量 Map m new HashMap 10 1 but after lots of works Here how to get current values 您不应该能够获得负载系数和容量 它们是
  • 为什么Java的“受保护”比默认的受保护要少? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何禁用 React Material UI 自动完成表单中的 ENTER 键

    我在 React 组件中有一个 Material UI 自动完成表单 它工作完美 除了 ENTER 键当前正在清除输入字段 我只是想要输入字段not当用户按 ENTER 键时被清除 我搜索了 Stackoverflow 上的所有类似问题 没
  • 返回最新“条纹”数据的行

    给出一个包含以下数据的简单表格 id result played 7 L 2012 01 07 6 L 2012 01 06 5 L 2012 01 05 4 W 2012 01 04 3 W 2012 01 03 2 L 2012 01
  • scipy.integrate.solve_ivp 不清楚如何求解形式 0=F(t, y(t), y'(t)) 的隐式 ODE

    目前 我确实使用assimulos 求解器套件 https jmodelica org assimulo tutorial imp html求解 0 F t y t y t 形式的隐式微分方程 我想使用本机 scipy 安装附带的求解器 并
  • Android页面卷曲动画

    有没有简单的方法来做Curl翻页动画 卷曲动画是页面翻转的动画 包括上面的页面滚动和下面的页面阴影 一次显示两页的 画廊 就像一本书一样 的推荐方法是什么 Is it 让适配器一次显示两个图像的线性布局 它不会让我像书一样显示一页翻过另一页
  • 为什么 echo 不返回与没有 echo 相同的结果

    我有以下案例 regex OK space alnum alnum text OK AAA BBBBBB aaabbbcccdddfffed asdadadadadadsada OK CCC KKKKKKK some text here O
  • C# 刷新 StreamWriter 和 MemoryStream

    我使用以下代码片段 我不确定是否需要调用Flush方法 一旦StreamWriter 一旦开启MemoryStream converts an xsd object to the corresponding xml string using
  • 如何在 Perl 中运行子命令正确导入环境?

    在从子命令导入环境时 我想将从 bash 脚本导出的所有环境变量添加到哈希中 什么时候program运行后 它将设置一些变量并导出它们 我想将这些变量保存在 Perl 脚本中供以后使用 但是我不想采用子命令中定义的 bash 函数 目前 我
  • 如何从 Java 获取 JanusGraphManagement

    我无法理解如何从使用ConfiguredGraphFactory 创建的图表中获取JanusGraphManagement 实例 我尝试做这样的事情 JanusGraphFactory Builder config JanusGraphFa
  • 更新 Popup.Animated 以播放 gif 直到外部任务完成 (PYSimpleGUI)

    我希望创建一个 UI 在执行另一项任务时显示动画弹出窗口 完成后将退出 我正在使用 PYSimpleGUI 并使用列出的示例here https github com PySimpleGUI PySimpleGUI blob master