Skip to content

async_move()

Luke edited this page Apr 15, 2020 · 1 revision

CanvasPlus.async_move()

Along with the default Canvas.move() method, asynchronous movement is also possible, using the asyncio library

from typing import Union, Tuple
from numbers import Real
async def async_move(
    self, tagOrId: Union[int, str], xDist: Real, yDist: Real, time: float, fps: int = 24, update: bool = True
) -> Tuple[Union[float, int]]

Asynchronously move tagOrId by xDist and yDist (x distance, y distance) fps: frames per second, time: specify the amount of time the animation shall take to complete, update: call update() method within loop

Example

# from CanvasPlus import CanvasPlus
from CanvasPlus import CanvasPlus
from tkinter import Tk
import math
import asyncio

#set up canvas
root = Tk()
canvas = CanvasPlus(root, width = 800, height = 800, background = "white")
canvas.pack()

rect1 = canvas.create_rectangle(0, 100, 200, 300, fill = "red")
rect2 = canvas.create_rectangle(0, 300, 200, 500, fill = "green")
rect3 = canvas.create_rectangle(0, 500, 200, 700, fill = "blue")

async def _test():
    await asyncio.gather(
        canvas.async_move(rect1, 600, -100, 2),
        canvas.async_move(rect2, 600, 0, 3),
        canvas.async_move(rect3, 600, 100, 4)
    )

asyncio.run(_test())
canvas.mainloop()

async_move Note: The frame rates in this GIF do not accurately represent the actual frame rates, due to the nature of GIF images

Clone this wiki locally