加快潜在的长期任务

时间:2017-12-16 23:51:20

标签: python tkinter

我决定制作一个类,以便在Tkinter中轻松显示动画GIF,这确实有效,但动态收集所有帧的过程总是花费大量的时间并防止其他任何事情发生,直到它完成。我想知道是否有任何方法可以加速它或以更有效的方式做同样的事情。

以下是该类的代码:

.xls

以下是它的使用方法:

from tkinter import *

class animation:
    def __init__(self,*args,**kwargs):
        self.root=args[0]
        self.label=Label(self.root)
        self.label.grid()
        self.image=kwargs["image"]
        try:
            self.delay=kwargs["delay"]
        except KeyError:
            self.delay=20

        self.frames=[]
        x=0
        while True:
            try:
                img=PhotoImage(file=self.image,
                               format="gif -index {}".format(x))
                self.frames.append(img)
                x+=1
            except:
                break


    def animate(self,y):
        try:
            self.label.configure(image=self.frames[y])
            self.root.after(self.delay,lambda:self.animate(y+1))
        except IndexError:
            self.label.configure(image=self.frames[0])
            self.root.after(self.delay,lambda:self.animate(1))

1 个答案:

答案 0 :(得分:0)

尝试使用线程:

import threading
import time

def __init__(self, *args, **kwargs):
    self.frames=[]
    self.run_loop = True
    self.x=0
    self.taken = []
    self.order = []

    while self.run_loop:
        threading.Thread(target=self.add_image).start()

def add_image(self):
    try:
        if self.x not in self.taken: # make sure not several threads add the same image index
            self.taken.append(self.x)
            self.x+=1
        else:
           return

        # save local x before creating image, for the order
        # all the other threads will increment self.x while Image is created
        x = self.x   

        img=PhotoImage(file=self.image, format="gif -index {}".format(self.x))

        self.frames.append(img)
        self.order.append(x) # keep track of the order

        if len(self.frames) == len(self.taken): # when finish
            self.frames = [x for _,x in sorted(zip(self.order,self.frames))]  # sort the frames to the order

    except:
        self.run_loop = False

我在没有tkinter的情况下创建了一个简单的可运行示例,使用time.sleep(随机时间量)来模拟PhotoImage:

import threading
import time
from random import randint

class Test:

    frames=[]
    run_loop = True
    x=0
    taken = []
    order = []
    time = 0

    def __init__(self):
        while self.run_loop:
            threading.Thread(target=self.add_image).start()

    def add_image(self):
        if self.x < 100:
            if self.x not in self.taken: # make sure not several threads add the same image index
                self.taken.append(self.x)
                self.x+=1
            else:
               return

            x = self.x
            t = randint(1,10)/10.0
            self.time += t

            time.sleep(t) # PhotoImage random time.sleep 0 to 1 second

            self.order.append(x)
            self.frames.append(x)

            if len(self.frames) == len(self.taken):
                print("Frames before sort")
                print(self.frames)

                self.frames = [x for _,x in sorted(zip(self.order,self.frames))]

                print("\nFrames after sort")
                print(self.frames)

                print("\nTime used combined: {} seconds".format(self.time))


        else:
            self.run_loop = False


t = Test()

此测试显示使用的总时间约为50秒 有100个线程,它在1秒内完成。最长时间的时间量。睡眠时间为0到1秒 所以对你来说,它不应该超过最长PhotoImage次呼叫