如何让tkinter画布对象在ci中移动

2024-05-04 12:56:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个画布创建的图像:

我在画布上画了一个圆圈:

生成此圆的代码:

def create_circle(x, y, r, canvasName): #center coordinates, radius
    x0 = x - r
    y0 = y - r
    x1 = x + r
    y1 = y + r
    return canvasName.create_oval(x0, y0, x1, y1, outline='red')
create_circle(100, 100, 50, canvas)

我想让画布创建的图像,以遵循画布绘制的圆圈准确(去圆),由每个像素。这怎么可能?你知道吗

为了详细说明,这里演示了我想要画布图像做什么:

https://i.gyazo.com/8218fe1c82008f2ed034a78b46f787e6.mp4


Tags: 代码图像def画布createcenterx1circle
1条回答
网友
1楼 · 发布于 2024-05-04 12:56:44

您可以使用root.after发送定期调用来更改图像的坐标。在那之后,只需计算每次调用中图像的新x,y位置。你知道吗

import tkinter as tk
from math import cos, sin, radians

root = tk.Tk()
root.geometry("500x500")

canvas = tk.Canvas(root, background="black")
canvas.pack(fill="both",expand=True)

image = tk.PhotoImage(file="plane.png").subsample(4,4)

def create_circle(x, y, r, canvasName):
    x0 = x - r
    y0 = y - r
    x1 = x + r
    y1 = y + r
    return canvasName.create_oval(x0, y0, x1, y1, outline='red')

def move(angle):
    if angle >=360:
        angle = 0
    x = 200 * cos(radians(angle))
    y = 200 * sin(radians(angle))
    angle+=1
    canvas.coords(plane, 250+x, 250+y)
    root.after(10, move, angle)

create_circle(250, 250, 200, canvas)
plane = canvas.create_image(450,250,image=image)

root.after(10, move, 0)

root.mainloop()

enter image description here

相关问题 更多 >