3

I have a simple ellipse (red) and I want to rotate the ellipse. The following code only return flatten ellipse (blue) but it does not rotate: import tkinter as tk import numpy as np import math

def rotate(points, angle):
    new_points = list(points)
    rad = angle * (math.pi/180)
    cos_val = math.cos(rad)
    sin_val = math.sin(rad)
    for coords in new_points:
        x_val =  coords[0] 
        y_val = coords[1]
        coords[0] = x_val * cos_val - y_val * sin_val
        coords[1] = x_val * sin_val + y_val * cos_val
    return new_points

window = tk.Tk()
window.geometry("500x300")
canvas = tk.Canvas(window, height=300, width=500)
canvas.grid(row=0, column=0, sticky='w')

# draw ellipse 
size=80
x0,y0=100,100
width,height=0.25*size,0.5*size
x1,y1=x0+width,y0+height
xc=x0+width/2
yc=y0+height/2
ellipse = canvas.create_oval([x0, y0, x1,y1],fill='blue')

#draw rotated ellipse
coord1=canvas.coords(ellipse)
points=[[coord1[0],coord1[1]],[coord1[2],coord1[3]]]
point2=rotate(points,30)
coord2 = [item for sublist in point2 for item in sublist]
ellipse2 = canvas.create_oval(coord2,fill='red')

window.mainloop ()

Here is the result:

red and blue ellipse. The blue one is supposed to be rotated

The red ellipse supposed to be rotated by 30 degree but instead of rotated, it just get flattened.

Question: How to rotate the ellipse in tkinter canvas?

Notes:

  • I am using python 3.6

  • I checked stackoverflow on similar question and it has no correct answer.

  • unlike polygon that we can simply rotate each vertices, ellipse has no vertex.

Hajar
  • 93
  • 1
  • 6
  • Create and draw polygon of oval and rotate the points: https://mail.python.org/pipermail/python-list/2000-December/022013.html – xaedes Mar 05 '19 at 18:41

2 Answers2

1

The oval item of tkinter canvas is not rotatable as an oval.

If you look at the docs, like effbot.org, you'll see that some items are created with a first position arg (a single point, like text), some with a bbox arg (two points, like rectangle and oval), and some with coords (variable, two or more points, like line and polygon).

You can rotate the coords items by (1) calculating new coords and (2) updating the item with the new coords, using the coords() method.

For the rest of the items, you're out of luck, except for text which supports an angle attribute which you can set with itemconfig(item, angle=new_angle)

But all hope is not lost. You can convert a rectangle or oval into a polygon (by creating a new polygon to replace your old bbox item, and then you'll be able to rotate the new item. With rectangle, it's easy-peasy. With oval, it's much trickier, as you have to simulate the oval using many, many polygon coordinates for it to look good.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32
0

There are 2 ways that I know of 1) plot every point on the edge of the ellipse. There are pages on the web that show how to calculate all of the points 2) save as an image and use tkinter's PIL library to rotate the image.