【问题标题】:How can i make a tkinter oval canvas revolve around a circle我怎样才能使 tkinter 椭圆画布围绕一个圆圈旋转
【发布时间】:2020-07-13 10:47:07
【问题描述】:

拜托,我正在尝试制作一个椭圆形画布以围绕一个圆圈旋转。我已经尽力了,但不知道如何。谁能帮我吗。查看代码

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()

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

【问题讨论】:

    标签: python tkinter canvas


    【解决方案1】:

    好像是一道简单的数学题,只要得到圆心,得到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,您也可以更改值以查看更改:

    【讨论】:

    • 谢谢。正是我想要的
    • 我知道R是弧度,那r呢?
    • 或者你能帮我解释一下吗
    • 哇,谢谢,我从早上开始就一直在尝试这个。即使用三角函数,但无济于事。我现在明白了
    猜你喜欢
    • 2019-04-25
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 2021-07-30
    • 1970-01-01
    • 2021-06-24
    • 2011-10-13
    相关资源
    最近更新 更多