Skip to content

rotate()

Luke edited this page Apr 12, 2020 · 5 revisions

CanvasPlus.rotate()

There's no easy way to rotate something in tkinter. Usually, there's a lot of math and trig that is put into it. This method uses complex numbers to rotate a polygon clockwise in radians or degrees.

from numbers import Real
from typing import Union
def rotate(self, tagOrId: Union[int, str], x: Real, y: Real, amount: Real, unit: str = "rad", warn: bool = True) -> Tuple[Union[float, int]]

Rotate obj on axis x, y by amount in degrees or radians clockwise. Turn warn off to prevent warnings. Returns new coords.

Example:

from tkinter import Tk
import math
from CanvasPlus import CanvasPlus
root = Tk()

canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

rect = canvas.create_rectangle(100, 100, 200, 200, fill="#f7a8c6", width=0)
rect = canvas.poly(rect)
canvas.rotate(rect, 150, 150, 45, unit = "d")

arrow = canvas.create_arrow(600, 600, 50, 50, 150, 20, fill = "grey", outline = "black")
canvas.rotate(arrow, 600, 600, math.pi/3)

round_rect = canvas.create_round_rectangle(300, 300, 400, 500, fill = "black", outline = "green", width = 1)
canvas.rotate(round_rect, 500, 500, 5, unit = "rad")

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-30 at 5 37 04 PM

Clone this wiki locally