【问题标题】:Python Tkinter Display Loading Animation While Performing Certain TasksPython Tkinter 在执行某些任务时显示加载动画
【发布时间】:2016-10-03 02:28:19
【问题描述】:

我从 https://www.daniweb.com/programming/software-development/threads/396918/how-to-use-animated-gifs-with-tkinter 找到了这个有用的 Tkinter 动画代码,由“vegaseat”提供。

我采用了类似的设计来向项目显示 gif 动画。我希望将此实现为脚本某些区域的功能,例如导入模块等。我尝试了一些方法,但是当我将其作为函数调用时,它首先运行动画,然后导入模块(正如我们所期望的那样)。

我想我正在探索让它同时工作的方法......当脚本正在导入模块(或运行我希望显示动画的另一个进程)时,动画将显示,然后消失,直到下一个电话。建议将不胜感激。

非常感谢。

# mimic an animated GIF displaying a series of GIFs
# an animated GIF was used to create the series of GIFs 
# with a common GIF animator utility

import time
from Tkinter import *
root = Tk()
imagelist = ["dog001.gif","dog002.gif","dog003.gif",
             "dog004.gif","dog005.gif","dog006.gif","dog007.gif"]
# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
    photo = PhotoImage(file=imagefile)
    giflist.append(photo)
# loop through the gif image objects for a while
for k in range(0, 1000):
    for gif in giflist:
        canvas.delete(ALL)
        canvas.create_image(width/2.0, height/2.0, image=gif)
        canvas.update()
        time.sleep(0.1)
root.mainloop()

编辑:根据一些有用的建议,我正在尝试实现下面的代码。目标是在应用程序导入“IMPORTS”函数中的模块时开始动画,然后在导入完成后将其销毁。

# Import modules
from Tkinter import *
from PIL import ImageTk
from PIL import Image
import os,time
from os.path import dirname
from os.path import join

def IMPORTS():
    import tkMessageBox
    from ttk import Combobox
    import csv,datetime
    import xlrd,xlwt
    import getpass
    import traceback
    import arcpy
    from arcpy import AddMessage
    import win32com.client


inGif = #root image (.gif)
FramesFolder = #Folder containing frames of the root image

W=Toplevel() 
W.wm_overrideredirect(True) # I wish to only display the widget spinning without the window frame

imagelist = [os.path.join(FramesFolder,s) for s in os.listdir(FramesFolder) if not s.endswith('db')]
# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(W,width=width, height=height)
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
    photo = PhotoImage(file=imagefile)
    giflist.append(photo)

timer_id = None

def start_loading(n=0):
    global timer_id
    gif = giflist[n%len(giflist)]
    canvas.create_image(gif.width()//2, gif.height()//2, image=gif)
    timer_id = W.after(100, start_loading, n+1) # call this function every 100ms

def stop_loading():
    if timer_id:
        W.after_cancel(timer_id)
        canvas.delete(ALL)

start_loading()
IMPORTS()
stop_loading()
# The spinning widget should be completely destroyed before moving on...

它正在返回

"NameError: name 'tkMessageBox' is not defined"

【问题讨论】:

    标签: python animation tkinter


    【解决方案1】:

    您可以使用Tk.after()Tk.after_cancel() 来启动和停止动画:

    timer_id = None
    
    def start_loading(n=0):
        global timer_id
        gif = giflist[n%len(giflist)]
        canvas.create_image(gif.width()//2, gif.height()//2, image=gif)
        timer_id = root.after(100, start_loading, n+1) # call this function every 100ms
    
    def stop_loading():
        if timer_id:
            root.after_cancel(timer_id)
            canvas.delete(ALL)
    

    那么,你可以在长进程前调用start_loading(),在长进程后调用stop_loading()

    start_loading()
    long_process() # your long process
    stop_loading()
    

    【讨论】:

    • 我试图调整您的建议,但现在它返回“图像“pyimage19”不存在”错误。另外,也许你可以解释这一行 timer_id = root.after(100, start_loading, n+1) # 每 100 毫秒调用一次这个函数。不确定您如何在其声明范围内调用 start_loading
    • @COCO root.after() 是设置超时。第一个参数是超时时间,第二个参数是要在超时时间调用的函数,其余参数传递给给定函数。因此,该语句是设置超时以在 100 毫秒后执行带有参数 n+1start_loading 函数。这意味着start_loading 将每 100 毫秒执行一次,直到超时被after_cancel() 函数取消。 start_loading 的参数n 用于选择要显示的图像。
    • 感谢您的澄清。我试图实施您的建议,并用代码更新了我的主要帖子。如前所述,它正在返回错误。请让我知道您认为我们可能需要更改的内容。非常感谢。
    • @COCO 你导入tkMessageBox 模块了吗?
    • 嗨,acw,不,消息框将与其他函数一起导入该函数...我认为不需要在启动动画之前导入消息框,因为它没有依赖关系,我是对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    相关资源
    最近更新 更多