welman ebimobowei
welman ebimobowei

Reputation: 29

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. 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

Upvotes: 0

Views: 377

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

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:

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:

The r and R,you could also change the value to see the changes: enter image description here

Upvotes: 2

Related Questions