Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw a point with Canvas in Tkinter?

I want to draw a point in Tkinter,Now I'm using Canvas to make it,but I didn't find such method to draw a point in Canvas class.Canvas provides a method called crete_line(x1,y1,x2,y2),so i tried to set x1=x2,y1=y2 to draw a point, but it doesn't work.

So anyone can tell me how to make it,it will be better if use Canvas can make it,other solution will be also accepted.Thanks!

like image 390
starkshang Avatar asked Oct 06 '16 06:10

starkshang


People also ask

Is it possible to draw a circle directly in tkinter canvas?

Drawing a circle on a tkinter Canvas is usually done by the create_oval method.

Which one of the following we can draw using canvas in tkinter?

A tkinter canvas can be used to draw in a window. Use this widget to draw graphs or plots. You can even use it to create graphical editors. You can draw several widgets in the canvas: arc bitmap, images, lines, rectangles, text, pieslices, ovals, polygons, ovals, polygons, and rectangles.

Which is the correct way to draw a line in canvas tkinter?

Tkinter Canvas widget can be used for multiple purposes such as drawing shapes, objects, creating graphics and images. To draw a line on a Canvas, we can use create_line(x,y,x1,y1, **options) method.


2 Answers

There is no method to directly put a point on Canvas. The method below shows points using create_oval method.

Try this:

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()
like image 140
trahane Avatar answered Oct 06 '22 23:10

trahane


The provided above solution doesn't seem to work for me when I'm trying to put a sequence of a few pixels in a row.

I've found another solution -- reducing the border width of the oval to 0:

canvas.create_oval(x, y, x, y, width = 0, fill = 'white')
like image 30
user223850 Avatar answered Oct 06 '22 22:10

user223850