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!
Drawing a circle on a tkinter Canvas is usually done by the create_oval method.
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.
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.
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()
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With