[英]How can i make a tkinter oval canvas revolve around a circle
Please, I am trying to make an oval canvas to revolve around a circle.拜托,我正在尝试制作一个椭圆形 canvas 围绕一个圆圈旋转。 I have tried all possible best but don't know how.
我已经尽力了,但不知道如何。 Can anyone help me out.
谁能帮我吗。 Check out the code
查看代码
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()
I want the bigger ball to move Along the lines of the circle我想让大球沿着圆圈的线移动
It seems it is an easy math problem, just get the center of circle and get the bbox can do it, use coords
to move it:这似乎是一道简单的数学题,只要得到圆心,得到 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()
output example: output 示例:
The r
and R
,you could also change the value to see the changes: r
和R
,您也可以更改值以查看更改:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.