首页 > 解决方案 > How can I delay the time of creating line in canvas in python?

问题描述

I want to create one line in canvas in one second and another line in another second. But my code shows all of the lines in one second after delaying 1 second. In every second I want to create only one line in canvas according to these (everysecond) coordinate. I am using for loop to create line 60 line inside the canvas.


from tkinter import *
import time

root = Tk()
canvas = Canvas(root)
canvas.pack()
everysecond = [(99), (27),
               (107), (29),
               (115), (29),
               (122), (33),
               (129), (35),
               (135), (38),
               (142), (43),
               (147), (48),
               (153), (52),
               (157), (57),
               (161), (64),
               (165), (72),
               (168), (79),
               (169), (85),
               (171), (92),
               (169), (99),
               (171), (108),
               (169), (115),
               (168), (123),
               (165), (130),
               (160), (134),
               (158), (142),
               (153), (147),
               (148), (154),
               (142), (158),
               (136), (160),
               (129), (166),
               (122), (168),
               (114), (170),
               (107), (170),
               (100), (169),
               (92), (169),
               (85), (170),
               (79), (168),
               (72), (166),
               (67), (160),
               (58), (158),
               (52), (153),
               (48), (148),
               (43), (142),
               (38), (134),
               (33), (128),
               (34), (121),
               (30), (114),
               (31), (107),
               (29), (102),
               (30), (91),
               (31), (86),
               (32), (79),
               (35), (70),
               (36), (70),
               (44), (58),
               (47), (53),
               (53), (47),
               (58), (41),
               (63), (37),
               (71), (34),
               (78), (32),
               (85), (30),
               (92), (29)]


def second():
    j = 1
    k = 0
    for i in range(0, 60):
      canvas.create_line((everysecond[k], everysecond[j], 100, 100), width=2, fill='red')
      j = j + 2
      k = k + 2
canvas.after(1000, second)
root.mainloop()

标签: pythontkintercanvastime

解决方案


Pass the k and j as the arguments in the after.like:

from tkinter import *
import time

root = Tk()
canvas = Canvas(root)
canvas.pack()

line_id = []

everysecond = [(99), (27),
               (107), (29),
               (115), (29),
               (122), (33),
               (129), (35),
               (135), (38),
               (142), (43),
               (147), (48),
               (153), (52),
               (157), (57),
               (161), (64),
               (165), (72),
               (168), (79),
               (169), (85),
               (171), (92),
               (169), (99),
               (171), (108),
               (169), (115),
               (168), (123),
               (165), (130),
               (160), (134),
               (158), (142),
               (153), (147),
               (148), (154),
               (142), (158),
               (136), (160),
               (129), (166),
               (122), (168),
               (114), (170),
               (107), (170),
               (100), (169),
               (92), (169),
               (85), (170),
               (79), (168),
               (72), (166),
               (67), (160),
               (58), (158),
               (52), (153),
               (48), (148),
               (43), (142),
               (38), (134),
               (33), (128),
               (34), (121),
               (30), (114),
               (31), (107),
               (29), (102),
               (30), (91),
               (31), (86),
               (32), (79),
               (35), (70),
               (36), (70),
               (44), (58),
               (47), (53),
               (53), (47),
               (58), (41),
               (63), (37),
               (71), (34),
               (78), (32),
               (85), (30),
               (92), (29)]


def second(k, j):
    line_id.append(canvas.create_line((everysecond[k], everysecond[j], 100, 100), width=2, fill='red'))
    if j == len(everysecond)-1:
        for id in line_id:
            canvas.delete(id)
        canvas.after(100, second, 0, 1)
    else:
        canvas.after(100, second, k + 2, j + 2)

canvas.after(100, second, 0, 1)
root.mainloop()

推荐阅读