我找到了這個有用的代碼爲從「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