简体   繁体   English

如何实时为 canvas 设置动画

[英]How to animate a canvas in real time

I want to change the fill colour of each zone(segment) of the decagon depending on each number is the list L.我想根据每个数字更改十边形的每个区域(段)的填充颜色是列表 L。

Currently it only displays the canvas at the end and using the last number in the list L.目前它仅在末尾显示 canvas 并使用列表 L 中的最后一个数字。

The idea is each time it reads a number from the list L it updates the canvas, fills that zone and changes the last filled zone back to white这个想法是每次从列表 L 中读取一个数字时,它都会更新 canvas,填充该区域并将最后填充的区域更改回白色

The zones ('zn') are from 0-5, 0 Being the inside decagon and 1-5 the outside.区域('zn')是从 0-5,0 是内侧十边形,1-5 是外侧。 and a combination of zones formatted 1_2, 2_3, 3_4, 4_5 and 5_1以及格式为 1_2、2_3、3_4、4_5 和 5_1 的区域组合

from tkinter import *
import time

root = Tk()
cWidth = 800
cHeight = 650
canvas = Canvas(root, width=cWidth, height=cHeight)
canvas.pack()

zn1 = canvas.create_polygon(257, 635, 319, 445, 269, 409, 107, 526,outline='blue',fill='white',tag='z1',)
zn12 = canvas.create_polygon(107, 526, 269, 409, 250, 350, 50, 350,outline='black',fill='white', tag='z1_2')
zn2 = canvas.create_polygon(50, 350, 250, 350, 269, 291, 107, 174,outline='purple',fill='white', tag='z2')
zn23 = canvas.create_polygon(107, 174, 269, 291, 319, 255, 257, 65,outline='black',fill='white', tag='z2_3')
zn3 = canvas.create_polygon(257, 65, 443, 65, 381, 255, 319, 255,outline='orange',fill='white', tag='z3')
zn34 = canvas.create_polygon(443, 65, 381, 255, 431, 291, 593, 174,outline='black',fill='white', tag='z3_4')
zn4 = canvas.create_polygon(593, 174, 431, 291, 450, 350, 650, 350,outline='cyan',fill='white', tag='z4')
zn45 = canvas.create_polygon(650, 350, 450, 350, 431, 409, 593, 526,outline='black',fill='white', tag='z4_5')
zn5 = canvas.create_polygon(593, 526, 431, 409, 381, 445, 443, 635,outline='green',fill='white', tag='z5')
zn51 = canvas.create_polygon(443, 635, 381, 445, 319, 445, 257, 635,outline='black',fill='white', tag='z5_1')
zn0 = canvas.create_polygon(381, 255, 319, 255, 269, 291, 250, 350, 269, 409, 319, 445, 381, 445, 431, 409, 450, 350,
                            431, 291,outline='black',fill='white', tag='z0')

L= ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '1_5', '1_5', '5', '5', '5', '5', '5', '5', '5', '5', '1', '1', '1', '1', '1', '1', '1_5', '5', '1',
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1_2', '2', '1_2', '0_1', '0_1', '0_1', '0_1', '1', '1',
        '0_1', '0', '0_1_2', '1_2', '1', '1_2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2_3',
        '2_3', '0_2_3', '0_2', '0_2', '0_2', '2', '2', '2', '2_3', '3', '2_3', '2', '2_3', '2_3_4', '3_4', '3', '3',
        '3_4', '3_4', '2_3_4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
        '3', '4', '3', '3', '3_4', '4', '4', '4', '4', '3_4', '4', '4', '0_3_4', '3_4', '3', '3', '3', '3', '0', '4',
        '0_4', '4', '4', '5', '4', '4', '4', '4', '4_5', '4_5', '4_5', '4_5', '5', '5', '5', '5', '5', '5', '0_5',
        '0_5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '1_5', '5', '5', '5', '1',
        '1_5', '0_5', '0', '0', '0', '0_1_5', '0_5', '0_5', '0_1', '0_1', '0', '1', '1_2', '0_2', '0_1', '0_1', '0_1',
        '2', '1_2', '1', '0_1_2', '0_1_2', '0_1_2', '1_3', '3', '0', '0_2', '2', '2', '0', '0_2', '2', '2', '0_3',
        '0_3', '0_3', '0', '0', '0', '0', '0', '3_4', '0_3_4', '0_3_4', '0', '0', '4', '5', '5', '0', '4_5', '4_5',
        '4_5', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '1']

def resetMap():
    canvas.itemconfig(zn0, fill='white')
    canvas.itemconfig(zn1, fill='white')
    canvas.itemconfig(zn12, fill='white')
    canvas.itemconfig(zn2, fill='white')
    canvas.itemconfig(zn23, fill='white')
    canvas.itemconfig(zn3, fill='white')
    canvas.itemconfig(zn34, fill='white')
    canvas.itemconfig(zn4, fill='white')
    canvas.itemconfig(zn45, fill='white')
    canvas.itemconfig(zn5, fill='white')
    canvas.itemconfig(zn51, fill='white')

def update(tkn):
    if len(tkn) == 1:
        if '0' in tkn:
            canvas.itemconfig(zn0, fill='red')
        elif '1' in tkn:
            canvas.itemconfig(zn1, fill='red')
        elif '2' in tkn:
            canvas.itemconfig(zn2, fill='red')
        elif '3' in tkn:
            canvas.itemconfig(zn3, fill='red')
        elif '4' in tkn:
            canvas.itemconfig(zn4, fill='red')
        elif '5' in tkn:
            canvas.itemconfig(zn5, fill='red')
    else:
        # run combination
        if '0' in tkn:
            canvas.itemconfig(zn0, fill='red')
        elif '1' in tkn and '2' in tkn:
            canvas.itemconfig(zn12, fill='red')
        elif '2' in tkn and '3' in tkn:
            canvas.itemconfig(zn23, fill='red')
        elif '3' in tkn and '4' in tkn:
            canvas.itemconfig(zn34, fill='red')
        elif '4' in tkn and '5' in tkn:
            canvas.itemconfig(zn45, fill='red')
        elif '5' in tkn and '1' in tkn:
            canvas.itemconfig(zn51, fill='red')

def task():
    for i in L:
        print(i)
        resetMap()
        tkn = i.split('_')
        update(tkn)
        time.sleep(0.1)

task()

root.mainloop()

Maybe your function used in a incorrect way.You could call .after instead of use time.sleep() .And don't use a for loop in the function,I revise your task() function:也许您的 function 使用方式不正确。您可以调用.after而不是使用time.sleep() 。并且不要在 function 中使用 for 循环,我会修改您的task() ZC1C125268E68354

from tkinter import *
import time

root = Tk()
cWidth = 800
cHeight = 650
canvas = Canvas(root, width=cWidth, height=cHeight)
canvas.pack()

zn1 = canvas.create_polygon(257, 635, 319, 445, 269, 409, 107, 526,outline='blue',fill='white',tag='z1',)
zn12 = canvas.create_polygon(107, 526, 269, 409, 250, 350, 50, 350,outline='black',fill='white', tag='z1_2')
zn2 = canvas.create_polygon(50, 350, 250, 350, 269, 291, 107, 174,outline='purple',fill='white', tag='z2')
zn23 = canvas.create_polygon(107, 174, 269, 291, 319, 255, 257, 65,outline='black',fill='white', tag='z2_3')
zn3 = canvas.create_polygon(257, 65, 443, 65, 381, 255, 319, 255,outline='orange',fill='white', tag='z3')
zn34 = canvas.create_polygon(443, 65, 381, 255, 431, 291, 593, 174,outline='black',fill='white', tag='z3_4')
zn4 = canvas.create_polygon(593, 174, 431, 291, 450, 350, 650, 350,outline='cyan',fill='white', tag='z4')
zn45 = canvas.create_polygon(650, 350, 450, 350, 431, 409, 593, 526,outline='black',fill='white', tag='z4_5')
zn5 = canvas.create_polygon(593, 526, 431, 409, 381, 445, 443, 635,outline='green',fill='white', tag='z5')
zn51 = canvas.create_polygon(443, 635, 381, 445, 319, 445, 257, 635,outline='black',fill='white', tag='z5_1')
zn0 = canvas.create_polygon(381, 255, 319, 255, 269, 291, 250, 350, 269, 409, 319, 445, 381, 445, 431, 409, 450, 350,
                            431, 291,outline='black',fill='white', tag='z0')

L= ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '1_5', '1_5', '5', '5', '5', '5', '5', '5', '5', '5', '1', '1', '1', '1', '1', '1', '1_5', '5', '1',
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1_2', '2', '1_2', '0_1', '0_1', '0_1', '0_1', '1', '1',
        '0_1', '0', '0_1_2', '1_2', '1', '1_2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2_3',
        '2_3', '0_2_3', '0_2', '0_2', '0_2', '2', '2', '2', '2_3', '3', '2_3', '2', '2_3', '2_3_4', '3_4', '3', '3',
        '3_4', '3_4', '2_3_4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
        '3', '4', '3', '3', '3_4', '4', '4', '4', '4', '3_4', '4', '4', '0_3_4', '3_4', '3', '3', '3', '3', '0', '4',
        '0_4', '4', '4', '5', '4', '4', '4', '4', '4_5', '4_5', '4_5', '4_5', '5', '5', '5', '5', '5', '5', '0_5',
        '0_5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '1_5', '5', '5', '5', '1',
        '1_5', '0_5', '0', '0', '0', '0_1_5', '0_5', '0_5', '0_1', '0_1', '0', '1', '1_2', '0_2', '0_1', '0_1', '0_1',
        '2', '1_2', '1', '0_1_2', '0_1_2', '0_1_2', '1_3', '3', '0', '0_2', '2', '2', '0', '0_2', '2', '2', '0_3',
        '0_3', '0_3', '0', '0', '0', '0', '0', '3_4', '0_3_4', '0_3_4', '0', '0', '4', '5', '5', '0', '4_5', '4_5',
        '4_5', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '5', '5', '1']

def resetMap():
    canvas.itemconfig(zn0, fill='white')
    canvas.itemconfig(zn1, fill='white')
    canvas.itemconfig(zn12, fill='white')
    canvas.itemconfig(zn2, fill='white')
    canvas.itemconfig(zn23, fill='white')
    canvas.itemconfig(zn3, fill='white')
    canvas.itemconfig(zn34, fill='white')
    canvas.itemconfig(zn4, fill='white')
    canvas.itemconfig(zn45, fill='white')
    canvas.itemconfig(zn5, fill='white')
    canvas.itemconfig(zn51, fill='white')

def update(tkn):
    if len(tkn) == 1:
        if '0' in tkn:
            canvas.itemconfig(zn0, fill='red')
        elif '1' in tkn:
            canvas.itemconfig(zn1, fill='red')
        elif '2' in tkn:
            canvas.itemconfig(zn2, fill='red')
        elif '3' in tkn:
            canvas.itemconfig(zn3, fill='red')
        elif '4' in tkn:
            canvas.itemconfig(zn4, fill='red')
        elif '5' in tkn:
            canvas.itemconfig(zn5, fill='red')
    else:
        # run combination
        if '0' in tkn:
            canvas.itemconfig(zn0, fill='red')
        elif '1' in tkn and '2' in tkn:
            canvas.itemconfig(zn12, fill='red')
        elif '2' in tkn and '3' in tkn:
            canvas.itemconfig(zn23, fill='red')
        elif '3' in tkn and '4' in tkn:
            canvas.itemconfig(zn34, fill='red')
        elif '4' in tkn and '5' in tkn:
            canvas.itemconfig(zn45, fill='red')
        elif '5' in tkn and '1' in tkn:
            canvas.itemconfig(zn51, fill='red')

index = 0

def task(i):
    if i == len(L): # check whether the index larger than the length of the list.
        return
    resetMap()
    tkn = L[i].split('_')
    update(tkn)
    root.after(100,task,i+1) # call this function per 0.1 second

root.after(100,task,index) # call after function and pass a index argument
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM