rotate() in Tkinter
Stephen D Evans
stevee at recombinant.demon.co.uk
Wed Dec 6 15:28:50 EST 2000
To rotate an oval convert the oval to a polygon and rotate that. Polygons are
easy to rotate, but this has to be done to the points before calling
create_polygon().
Referring to Tk documentation - The canvas does not support rotation.
'Practical Programming in Tcl and Tk', 3rd edition, 2000, by Brent B. Welsh,
publisher Prentice Hall PTR
This is a simple example.
(There are better methods of creating the points for an oval. Try fast
generation of ellipsoids in 'Graphics Gems V', 1995, edited by Alan W Paeth,
publisher AP Professional)
#
#
import math
import Tkinter
def poly_oval(x0,y0, x1,y1, steps=20, rotation=0):
"""return an oval as coordinates suitable for create_polygon"""
# x0,y0,x1,y1 are as create_oval
# rotation is in degrees anti-clockwise, convert to radians
rotation = rotation * math.pi / 180.0
# major and minor axes
a = (x1 - x0) / 2.0
b = (y1 - y0) / 2.0
# center
xc = x0 + a
yc = y0 + b
point_list = []
# create the oval as a list of points
for i in range(steps):
# Calculate the angle for this step
# 360 degrees == 2 pi radians
theta = (math.pi * 2) * (float(i) / steps)
x1 = a * math.cos(theta)
y1 = b * math.sin(theta)
# rotate x, y
x = (x1 * math.cos(rotation)) + (y1 * math.sin(rotation))
y = (y1 * math.cos(rotation)) - (x1 * math.sin(rotation))
point_list.append(round(x + xc))
point_list.append(round(y + yc))
return point_list
import Tkinter
root = Tkinter.Tk()
canvas = Tkinter.Canvas(root, width=400, height=400)
dict = {}
dict['outline'] = 'black'
dict['fill'] = 'yellow'
dict['smooth'] = 'true'
# use a polygon to draw an oval rotated 30 degrees anti-clockwise
apply(canvas.create_polygon, tuple(poly_oval(40,40, 200,300, rotation=30)),
dict)
canvas.pack()
root.mainloop()
-- Stephen D Evans
Dethe Elza wrote:
> I'm trying to create a complex cursor in Tkinter. I want to have an image
> which I can rotate, change the color of, and move about. I can build it as
> a vector image, but I use ovals. I haven't found any way to rotate images,
> vector drawings, or ovals in Tk.
>
> TIA
>
> --Dethe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20001206/0006828b/attachment.html>
More information about the Python-list
mailing list