Tkinter understanding after()

谁说我不能喝 提交于 2019-12-04 10:54:18

Time in time.sleep is in seconds whereas in after() it is in milliseconds. time.sleep(0.01) is the same as self.canvas.after(10, self.draw). If after(2, func) was too slow but after(1, func) was too fast then you could try sleep(0.0005) then after(1, func) do give a delay of 1.5 milliseconds but it is barely noticeable. Either way, fiddle with the timing until the pause is right.

Speed of the object (canvas) and clock/loop speed should be considered as two different things, IMHO. Hence, you can leave loop after 2ms or even larger like 10...25...

...
self.canvas.after(2, self.draw)
... # loop this after 2 ms.

while, changing speed in terms of 'how many pixels' :

pos = self.canvas.coords(self.id)
    if pos[1] <= 0:
        self.y = 20
    if pos[3] >= self.canvas_height:
        self.y = -20

so adjusting these values, and :

self.canvas.move(self.id, 245, 100)

can let you fine-tune position and speed of your red dot. Hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!