如何在Tkinter中同时运行两个图像循环

时间:2016-06-02 08:14:10

标签: python tkinter

我试图通过标签在Tkinter中同时运行两个图像循环。我尝试使用Process函数,但我想我必须找到一种方法来摆脱第一个循环并开始第二个循环。有这样的方式吗?

import time
import Tkinter
from multiprocessing import Process

side = Tkinter.Tk()

side.title('dice')
side.geometry('200x200')


def generate(x, y):
    return ["dice/" + str(i) + '.gif' for i in range(x, y)]

a1 = generate(1,13)
a2 = generate(1,15)
a3 = generate(1,17)
a4 = generate(1,19)
a5 = generate(1,21)
a6 = generate(1,23)



L1 = Tkinter.Label(side)
L2 = Tkinter.Label(side)
L1.pack()
L2.pack()


giflist_1 = list()
giflist_2 = list()

def anim(x):

for i in x:
    photo = Tkinter.PhotoImage(file= i)
    giflist_1.append(photo)

def anim_1(y):
    for i in y:
        photo = Tkinter.PhotoImage(file=i)
        giflist_2.append(photo)

def anim_3():
    for gif in giflist_2:
        L2.config(image=gif)
        L2.update()
        time.sleep(0.1)

def anim_2():
    for gif in giflist_1:
        L1.config(image=gif)
        L1.update()
        time.sleep(0.1)

if __name__ == '__main__':
    p1 = Process(target=anim(a1))
    p2 = Process(target=anim_1(a2))

    p1.start()
    p2.start()
    p3 = Process(target = anim_2())
    p4 = Process(target=anim_3())
    p3.start()
    p4.start()


side.mainloop()

1 个答案:

答案 0 :(得分:0)

您无需使用Process执行此简单任务。你可以做after方法。 根据您的代码,animanim_1只是加载图片,因此可以在开头执行。您可以在方法之后调用的其他2个anim函数。

...
def anim_3(gifID = 0):                              # Something to track on what GIF we are currently
    if gifID >= len(giflist_2): return              # No more gifs to display
    L2.config(image=giflist_2[gifID])
    L2.update()
    side.after(100, lambda: anim_3(gifID+1))        # Going to next GIF

def anim_2(gifID = 0):
    if gifID >= len(giflist_1): return
    L1.config(image=giflist_1[gifID])
    L1.update()
    side.after(100, lambda: anim_2(gifID+1))        # Number 100 means 100 milliseconds. It's equal to 0.1 seconds.

if __name__ == '__main__':
    anim(a1)
    anim_1(a2)

    side.after(100, anim_2)
    side.after(100, anim_3)

side.mainloop()