繁体   English   中英

我怎样才能使 tkinter 椭圆形 canvas 围绕一个圆圈旋转

[英]How can i make a tkinter oval canvas revolve around a circle

拜托,我正在尝试制作一个椭圆形 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()

我想让大球沿着圆圈的线移动

这似乎是一道简单的数学题,只要得到圆心,得到 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 示例:

rR ,您也可以更改值以查看更改: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM