我找到了来自https://www.daniweb.com/programming/software-development/threads/396918/how-to-use-animated-gifs-with-tkinter的Tkinter动画的有用代码,由#34; 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"
答案 0 :(得分: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()