[英]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 示例:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.