python - 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()
解决方案
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()
推荐阅读
- ruby-on-rails - Rails、Google OAuth 和 2 个不同的登录范围(管理员/前台)
- java - 对象+列表中的对象列表?
- azure-managed-identity - 用户管理身份 - 如何使用 c# 进行身份验证
- jquery - 如何在 Rails find_by_sql 和 group_by 中传递变量
- ruby-on-rails - 如何将我的主页与其他页面连接起来?
- reactjs - 如何使用谷歌材料 UI 为菜单项编写测试用例
- jolt - 按条件进行 JOLT 变换
- assembly - 在 8086 中添加两个 8 位数字给出错误答案
- jsf - PrimeFaces commandButton 在提交后不起作用
- python - Python 装饰器将实例初始化变量转换为自身属性