如何阻止 tkinter canvas widget / after 方法跳过列表中的元素?
How to stop tkinter canvas widget / after method from skipping elements in a list?
我正在尝试创建每秒更新一次的行。此更新需要显示在屏幕上,因此您应该能够在 window 中看到更改。例如,下面的每一行都应该在一秒钟后 运行。
canvas.create_line(1, 2, 10, 20, smooth="true")
canvas.create_line(1, 2, 10, 20, 50, 60, smooth="true")
canvas.create_line(1, 2, 10, 20, 50, 60, 100, 110, smooth="true")
这是我目前拥有的:
def make_line(index):
while index < len(database):
x, y, z = database[index]
# database is a list of tuples with numbers for coordinates
coordinates.append(x) # coordinates is an empty list
coordinates.append(y)
#don't need z since 2D map
index += 1
if index == 2:
# noinspection PyGlobalUndefined
global line
line = canvas.create_line(coordinates, smooth="true")
# same as canvas.create_line(1, 2, 10, 20, smooth="true")
elif index > 2:
canvas.after(1000, lambda: canvas.coords(line, coordinates))
# it's jumping from the 1st to the 4th element
else:
pass
make_line(0)
我认为问题出在 canvas.after 方法和 canvas.coords
到 运行 索引 = 3 的那一行时,坐标已经有 1、2、10、20、50、60、100 和 110,而它应该只有 1、2、10、20、50和 60。
提前致谢。
我最终不得不将 while 循环移出函数并添加 root.update_idletasks() 和 root.update() 而不是 root.mainloop()。从 我了解到你的程序基本上会在 mainloop 停止,而 update 将允许程序继续。
这就是我最终得到的:
def add_coordinates(index):
x, y, z = database[index]
coordinates.append(x) # coordinates is an empty list
coordinates.append(y)
ind = 0 # ind means index
while ind < len(database):
if ind < 2:
add_coordinates(ind)
elif ind == 2:
trajectory = canvas.create_line(coordinates, smooth="true")
add_coordinates(ind)
canvas.after(1000)
elif ind > 2:
add_coordinates(ind)
canvas.after(1000, canvas.coords(trajectory, coordinates))
else:
pass
ind += 1
root.update_idletasks()
root.update()
当然,我在文件的开头有 import 语句,在文件的最后有 root.mainloop()。
我正在尝试创建每秒更新一次的行。此更新需要显示在屏幕上,因此您应该能够在 window 中看到更改。例如,下面的每一行都应该在一秒钟后 运行。
canvas.create_line(1, 2, 10, 20, smooth="true")
canvas.create_line(1, 2, 10, 20, 50, 60, smooth="true")
canvas.create_line(1, 2, 10, 20, 50, 60, 100, 110, smooth="true")
这是我目前拥有的:
def make_line(index):
while index < len(database):
x, y, z = database[index]
# database is a list of tuples with numbers for coordinates
coordinates.append(x) # coordinates is an empty list
coordinates.append(y)
#don't need z since 2D map
index += 1
if index == 2:
# noinspection PyGlobalUndefined
global line
line = canvas.create_line(coordinates, smooth="true")
# same as canvas.create_line(1, 2, 10, 20, smooth="true")
elif index > 2:
canvas.after(1000, lambda: canvas.coords(line, coordinates))
# it's jumping from the 1st to the 4th element
else:
pass
make_line(0)
我认为问题出在 canvas.after 方法和 canvas.coords 到 运行 索引 = 3 的那一行时,坐标已经有 1、2、10、20、50、60、100 和 110,而它应该只有 1、2、10、20、50和 60。 提前致谢。
我最终不得不将 while 循环移出函数并添加 root.update_idletasks() 和 root.update() 而不是 root.mainloop()。从
def add_coordinates(index):
x, y, z = database[index]
coordinates.append(x) # coordinates is an empty list
coordinates.append(y)
ind = 0 # ind means index
while ind < len(database):
if ind < 2:
add_coordinates(ind)
elif ind == 2:
trajectory = canvas.create_line(coordinates, smooth="true")
add_coordinates(ind)
canvas.after(1000)
elif ind > 2:
add_coordinates(ind)
canvas.after(1000, canvas.coords(trajectory, coordinates))
else:
pass
ind += 1
root.update_idletasks()
root.update()
当然,我在文件的开头有 import 语句,在文件的最后有 root.mainloop()。