2016-10-03 13 views
0

"vegaseat"によって提供されるhttps://www.daniweb.com/programming/software-development/threads/396918/how-to-use-animated-gifs-with-tkinterのTkinterアニメーションにこの便利なコードがあります。Python Tkinter表示特定のタスクを実行中にアニメーションを読み込む

私はgifsアニメーションをプロジェクトに表示するために同様のデザインを採用しました。私はこれをスクリプトの特定の領域に関数として実装したいと考えています。私はいくつかのアプローチを試みましたが、これを関数として呼び出すと、最初にアニメーションが実行され、次にモジュールがインポートされます(期待どおり)。

私はこれを同時に実行する方法を模索していると思います...スクリプトがモジュールをインポートしている間(またはアニメーションを表示したい別のプロセスを実行中)、アニメーションが表示されてから消えるまで次の呼び出し。提案をいただければ幸いです。

ありがとうございます。

# 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() 

EDIT:以下のコードをいくつかの参考にして実装しようとしています。目的は、アプリケーションが "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... 

それはあなたがアニメーションを開始および停止するTk.after()Tk.after_cancel()を使用することができます

"NameError: name 'tkMessageBox' is not defined" 

答えて

0

を返して:

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() 
+0

私はあなたの提案を適応しようとしましたが、今は "画像"を返していますpyimage19 "存在しません"というエラーです。また、おそらくあなたはこの行を説明することができますtimer_id = root.after(100、start_loading、n + 1)#この関数を100msごとに呼び出します。宣言的スコープ内でstart_loadingをどのように呼び出すのかわからない – COCO

+0

@COCO 'root.after()'はタイムアウトを設定することです。最初の引数はタイムアウト期間、2番目の引数はタイムアウト時に呼び出される関数で、残りの引数は指定された関数に渡されます。したがって、100ms後に引数 'n + 1 'で' start_loading'関数を実行するタイムアウトを設定します。つまり、 'start_loading'は' after_cancel() '関数によってタイムアウトがキャンセルされるまで100msごとに実行されます。 'start_loading'の引数' n'は、どの画像を表示するかを選択するために使用されます。 – acw1668

+0

説明をありがとうございます。私はあなたの提案を実装しようとし、私のメインポストをコードで更新しました。上記のようにエラーが返されます。あなたが私たちが変わる必要があると思うものを私に教えてください。どうもありがとう。 – COCO

関連する問題