(Tkinter,py3)如何制作旋转动画?

时间:2017-11-05 13:22:57

标签: python-3.x tkinter tkinter-canvas

我想在它的中心周围旋转一个椭圆形。目前我可以显示静态对象witohut旋转,我似乎无法找到做什么机智。

编辑:我基于复数乘法制作了一个旋转函数,它似乎适用于较低的多边形,但椭圆有点看起来相同。

马铃薯级菜鸟C.S.学生。

另外,代码:

from tkinter import*
import cmath, math
import time


def poly_rot(tup, deg):
    rad=deg*0.0174533
    crot=cmath.exp(rad*1j)
    rotpol=[]
    i=0
    while i < len(tup):
        x=tup[i]
        y=tup[i+1]
        z=crot*complex(x,y)
        rotpol.append(400+z.real)
        rotpol.append(400+z.imag)
        i=i+2
    return rotpol


def poly_oval(x0,y0, x1,y1, steps=60, 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




inp= input("We need an ellipse. One to fit in a 800*800 window. Give the size of it's axes separated by ','-s (ex: lx,ly)!\n")
inpu= inp.split(',')
lx=int(inpu[0])
ly=int(inpu[1])
x0=400-lx//2
x1=400+lx//2
y0=400-ly//2
y1=400+ly//2
deg= float(input("Let's rotate this ellipse! But how much, in degrees, should we?"))

pre=poly_oval(x0, y0, x1, y1)
post=poly_rot(poly_oval(x0, y0, x1, y1), deg)



root =Tk()
w = Canvas(root, width=801, height=801)
w.config(background="dark green")
w.pack()
w.create_polygon(tuple(post), fill="yellow", outline="yellow" )




root.mainloop()

0 个答案:

没有答案