更新 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) 的相关文章

  • 使用 psycopg2 在 python 中执行查询时出现“编程错误:语法错误位于或附近”

    我正在运行 Python v 2 7 和 psycopg2 v 2 5 我有一个 postgresql 数据库函数 它将 SQL 查询作为文本字段返回 我使用以下代码来调用该函数并从文本字段中提取查询 cur2 execute SELECT
  • 通过 Scrapy 抓取 Google Analytics

    我一直在尝试使用 Scrapy 从 Google Analytics 获取一些数据 尽管我是一个完全的 Python 新手 但我已经取得了一些进展 我现在可以通过 Scrapy 登录 Google Analytics 但我需要发出 AJAX
  • 将 Matplotlib 误差线放置在不位于条形中心的位置

    我正在 Matplotlib 中生成带有错误栏的堆积条形图 不幸的是 某些层相对较小且数据多样 因此多个层的错误条可能重叠 从而使它们难以或无法读取 Example 有没有办法设置每个误差条的位置 即沿 x 轴移动它 以便重叠的线显示在彼此
  • 通过最小元素比较对 5 个元素进行排序

    我必须在 python 中使用元素之间的最小比较次数来建模对 5 个元素的列表进行排序的执行计划 除此之外 复杂性是无关紧要的 结果是一个对的列表 表示在另一时间对列表进行排序所需的比较 我知道有一种算法可以通过 7 次比较 总是在元素之间
  • Django:按钮链接

    我是一名 Django 新手用户 尝试创建一个按钮 单击该按钮会链接到我网站中的另一个页面 我尝试了一些不同的例子 但似乎没有一个对我有用 举个例子 为什么这不起作用
  • Flask 会话变量

    我正在用 Flask 编写一个小型网络应用程序 当两个用户 在同一网络下 尝试使用应用程序时 我遇到会话变量问题 这是代码 import os from flask import Flask request render template
  • 从字符串中删除识别的日期

    作为输入 我有几个包含不同格式日期的字符串 例如 彼得在16 45 我的生日是1990年7月8日 On 7 月 11 日星期六我会回家 I use dateutil parser parse识别字符串中的日期 在下一步中 我想从字符串中删除
  • 如何在 Python 中检索 for 循环中的剩余项目?

    我有一个简单的 for 循环迭代项目列表 在某些时候 我知道它会破裂 我该如何退回剩余的物品 for i in a b c d e f g try some func i except return remaining items if s
  • python 相当于 R 中的 get() (= 使用字符串检索符号的值)

    在 R 中 get s 函数检索名称存储在字符变量 向量 中的符号的值s e g X lt 10 r lt XVI s lt substr r 1 1 X get s 10 取罗马数字的第一个符号r并将其转换为其等效整数 尽管花了一些时间翻
  • 如何使用Python创建历史时间线

    So I ve seen a few answers on here that helped a bit but my dataset is larger than the ones that have been answered prev
  • IO 密集型任务中的 Python 多线程

    建议仅在 IO 密集型任务中使用 Python 多线程 因为 Python 有一个全局解释器锁 GIL 只允许一个线程持有 Python 解释器的控制权 然而 多线程对于 IO 密集型操作有意义吗 https stackoverflow c
  • Jupyter Notebook 内核一直很忙

    我已经安装了 anaconda 并且 python 在 Spyder IPython 等中工作正常 但是我无法运行 python 笔记本 内核被创建 它也连接 但它始终显示黑圈忙碌符号 防火墙或防病毒软件没有问题 我尝试过禁用两者 我也无法
  • 对年龄列进行分组/分类

    我有一个数据框说df有一个柱子 Ages gt gt gt df Age 0 22 1 38 2 26 3 35 4 35 5 1 6 54 我想对这个年龄段进行分组并创建一个像这样的新专栏 If age gt 0 age lt 2 the
  • 类型错误:预期单个张量时的张量列表 - 将 const 与 tf.random_normal 一起使用时

    我有以下 TensorFlow 代码 tf constant tf random normal time step batch size 1 1 我正进入 状态TypeError List of Tensors when single Te
  • 如何计算 pandas 数据帧上的连续有序值

    我试图从给定的数据帧中获取连续 0 值的最大计数 其中包含来自 pandas 数据帧的 id date value 列 如下所示 id date value 354 2019 03 01 0 354 2019 03 02 0 354 201
  • 发送用户注册密码,django-allauth

    我在 django 应用程序上使用 django alluth 进行身份验证 注册 我需要创建一个自定义注册表单 其中只有一个字段 电子邮件 密码将在服务器上生成 这是我创建的表格 from django import forms from
  • 导入错误:没有名为 site 的模块 - mac

    我已经有这个问题几个月了 每次我想获取一个新的 python 包并使用它时 我都会在终端中收到此错误 ImportError No module named site 我不知道为什么会出现这个错误 实际上 我无法使用任何新软件包 因为每次我
  • Python Selenium:如何在文本文件中打印网站上的值?

    我正在尝试编写一个脚本 该脚本将从 tulsaspca org 网站获取以下 6 个值并将其打印在 txt 文件中 最终输出应该是 905 4896 7105 23194 1004 42000 放置的动物 的 HTML span class
  • 如何使用 Pycharm 安装 tkinter? [复制]

    这个问题在这里已经有答案了 I used sudo apt get install python3 6 tk而且效果很好 如果我在终端中打开 python Tkinter 就可以工作 但我无法将其安装在我的 Pycharm 项目上 pip
  • NotImplementedError:无法将符号张量 (lstm_2/strided_slice:0) 转换为 numpy 数组。时间

    张量流版本 2 3 1 numpy 版本 1 20 在代码下面 define model model Sequential model add LSTM 50 activation relu input shape n steps n fe

随机推荐

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

    我在内存中有一个数据块 从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