如何让 canvas_create_oval 成为一个按钮?我可以在哪里点击它并执行命令 Python

How do I make canvas_create_oval a button? Where I can click on it and a command will be executed Python

如何将其设为按钮。

circle=scanvas.create_oval(440,220,310,90, activeoutline="black",outline="white", fill="purple", width="5")

您可以通过侦听 canvas 上的点击事件并使用 tag_bind 方法将其标记到元素,使 canvas 上绘制的元素表现得像一个按钮如下代码所示。

from tkinter import Tk, Canvas, messagebox

 top = Tk()

 def callback(event):
    messagebox.showinfo('Hello World','Hello Python')

 canvas = Canvas(top,bg="white",height=250,width=250)
 circle = canvas.create_oval(10,10,80,80,outline="black",fill="red")

 canvas.tag_bind(circle,"<Button-1>",callback)

 canvas.pack()
 top.mainloop()

<Button-1> 监听鼠标按下。您可以了解有关不同事件的更多信息 here