如何在 Tkinter 中使用 Canvas 绘制点?

sta*_*ang 7 python canvas tkinter

我想在Tkinter中画一个点,现在我正在使用Canvas它,但我没有找到这种在Canvas课堂上画一个点的方法。Canvas提供了一个名为 的方法crete_line(x1,y1,x2,y2),所以我试图设置x1=x2,y1=y2绘制一个点,但它不起作用。

所以任何人都可以告诉我如何制作它,如果使用Canvas可以制作它会更好,其他解决方案也将被接受。谢谢!

小智 5

没有办法直接在上面加点Canvas。下面的方法显示了要点使用create_oval方法。

尝试这个:

from Tkinter import *

canvas_width = 500
canvas_height = 150


def paint(event):
    python_green = "#476042"
    x1, y1 = (event.x - 1), (event.y - 1)
    x2, y2 = (event.x + 1), (event.y + 1)
    w.create_oval(x1, y1, x2, y2, fill=python_green)



master = Tk()
master.title("Points")
w = Canvas(master,
           width=canvas_width,
           height=canvas_height)
w.pack(expand=YES, fill=BOTH)
w.bind("<B1-Motion>", paint)

message = Label(master, text="Press and Drag the mouse to draw")
message.pack(side=BOTTOM)

mainloop()
Run Code Online (Sandbox Code Playgroud)


小智 5

当我尝试将几个像素的序列排成一行时,上面提供的解决方案似乎对我不起作用。

我找到了另一种解决方案——将椭圆形的边框宽度减小到 0:

canvas.create_oval(x, y, x, y, width = 0, fill = 'white')
Run Code Online (Sandbox Code Playgroud)


Cha*_*tor 5

使用 create_line 您还有其他可能的解决方案:

canvas.create_line(x, y, x+1, y, fill="#ff0000")
Run Code Online (Sandbox Code Playgroud)

它仅覆盖单个像素(x,y 到红色)