我找到了这个有用的代码为从“vegaseat”提供https://www.daniweb.com/programming/software-development/threads/396918/how-to-use-animated-gifs-with-tkinter Tkinter动画。Python Tkinter显示加载动画,同时执行某些任务
我已经改编了一个类似的设计来显示一个项目的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"
我试图适应你的建议,但现在它返回“图像”pyimage19“不存在”的错误。另外,也许你可以解释这行timer_id = root.after(100,start_loading,n + 1)#每100ms调用一次该函数。不知道你是如何在其声明范围内调用start_loading – COCO
@COCO'root.after()'是要设置超时。第一个参数是超时时间,第二个参数是要在超时时间调用的函数,其余参数传递给给定的函数。因此,该语句是设置超时以在100ms后执行具有参数“n + 1”的'start_loading'函数。这意味着'start_loading'将会每隔100ms执行一次,直到timeout被'after_cancel()'函数取消。 'start_loading'的参数'n'用于选择要显示的图像。 – acw1668
谢谢你的澄清。我试图实施你的建议,并用代码更新了我的主文章。正如所指出的那样,它正在返回错误。请让我知道你认为我们可能需要改变。非常感谢。 – COCO