当您在 python 中左键单击时,如何使对象出现在 canvas 中?
How to make an object appear in the canvas when you left click in python?
所以我想要的是在我的 python 作品的 canvas 上画一个椭圆。 (这是一个照片编辑器项目)。 "c" 指的是 canvas 我在 python 中制作并塑造成一个 tkinter 程序。我如何使用以下代码在程序的 canvas 中弹出一个椭圆形?(此外,如果您知道如何执行鼠标按下事件,请将“”更改为适当的标签):
def PaintBrushWorking():
blueBlob = c.create_oval(20, 30, 40, 60, fill = "blue")
blueBlob.pack()
c.bind_all("<Button-1>", PaintBrushWorking)
您只需删除对 pack
的调用,然后让您的函数接受一个事件参数。最后,您可能希望使用 bind
而不是 bind_all
,除非您真的希望它绘制椭圆形,即使您单击了一些其他小部件(例如按钮或滚动条)。
import Tkinter as tk
def PaintBrushWorking(event):
blueBlob = c.create_oval(20, 30, 40, 60, fill="blue")
root = tk.Tk()
c = tk.Canvas()
c.pack(fill="both", expand=True)
c.bind("<Button-1>", PaintBrushWorking)
root.mainloop()
所以我想要的是在我的 python 作品的 canvas 上画一个椭圆。 (这是一个照片编辑器项目)。 "c" 指的是 canvas 我在 python 中制作并塑造成一个 tkinter 程序。我如何使用以下代码在程序的 canvas 中弹出一个椭圆形?(此外,如果您知道如何执行鼠标按下事件,请将“
def PaintBrushWorking():
blueBlob = c.create_oval(20, 30, 40, 60, fill = "blue")
blueBlob.pack()
c.bind_all("<Button-1>", PaintBrushWorking)
您只需删除对 pack
的调用,然后让您的函数接受一个事件参数。最后,您可能希望使用 bind
而不是 bind_all
,除非您真的希望它绘制椭圆形,即使您单击了一些其他小部件(例如按钮或滚动条)。
import Tkinter as tk
def PaintBrushWorking(event):
blueBlob = c.create_oval(20, 30, 40, 60, fill="blue")
root = tk.Tk()
c = tk.Canvas()
c.pack(fill="both", expand=True)
c.bind("<Button-1>", PaintBrushWorking)
root.mainloop()