如何使tkinter椭圆形画布绕圆旋转

时间:2020-07-13 10:47:07

标签: python tkinter canvas

请,我正在尝试制作一个椭圆形的画布以绕一圈旋转。我已尽力而为,但不知道如何。谁能帮我吗。签出代码

from tkinter import *
import time
import math


root=Tk()
width=700
height=600
cn=Canvas(root,width=width,height=width)
cn.pack(expand=True,fill="both")

ball1=cn.create_oval(200,200,500,500)
ball2=cn.create_oval(200,200,300,300,fill="black")
ball3=cn.create_oval(330,330,370,370,fill="black")
l1=cn.create_line(350,180,350,600)
l2=cn.create_line(180,350,600,350)
pos1=cn.coords(ball3)

rect=cn.create_rectangle(100,100,700,600)

root.mainloop()

我希望更大的球沿着圆线移动

1 个答案:

答案 0 :(得分:2)

似乎这是一个简单的数学问题,只需获取圆心并让bbox可以做到,请使用coords移动它:

from tkinter import *
import time
import math


def moveCircle(angle=[0.0]):
    r = 50
    R = 150
    center_x, center_y = R * math.cos(math.radians(angle[0])) + 350, R * math.sin(math.radians(angle[0])) + 350
    cn.coords(ball2, center_x-r, center_y-r, center_x+r, center_y+r)
    angle[0] += 1.0
    root.after(100, moveCircle)

root = Tk()
width = 700
height = 600
cn = Canvas(root, width=width, height=width)
cn.pack(expand=True, fill="both")

ball1 = cn.create_oval(200, 200, 500, 500)
ball2 = cn.create_oval(200, 200, 300, 300, fill="black")
ball3 = cn.create_oval(330, 330, 370, 370, fill="black")
l1 = cn.create_line(350, 180, 350, 600)
l2 = cn.create_line(180, 350, 600, 350)

rect = cn.create_rectangle(100, 100, 700, 600)

root.after(10, moveCircle)
root.mainloop()

输出示例:

rR,您还可以更改值以查看更改: enter image description here