How to draw lines on canvas?


monkey 334

I've read some tutorials on the internet but I can't seem to find any way to show me the lines

Can someone help?

i try to do

p = Canvas(height = 600, width = 800).place(x=0,y=0)
p.create_rectangle(50, 25, 150, 75, fill="blue")

Unfortunately, it didn't work.

tobias_k

Not entirely sure what you're asking, since you neither showed us your full code, nor did you say anything "completely useless". It looks like you've figured out how to draw rectangles, and the same tutorial should have some knowledge about drawing lines, like the ones linked in the comments .

Since this doesn't seem to help you, the likely problem is that you are using Python 3, the Tkinterpackage has been renamed to tkinter. This example should work for you:

import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()

for i in range(10):
    canvas.create_line(50 * i, 0, 50 * i, 400)
    canvas.create_line(0, 50 * i, 400, 50 * i)
canvas.create_rectangle(100, 100, 200, 200, fill="blue")
canvas.create_line(50, 100, 250, 200, fill="red", width=10)

root.mainloop()

Addendum: I just noticed two real problems with your code:

  • This way p = Canvas(height = 600, width = 800).place(x=0,y=0), the variable pwill not be assigned Canvas, but the return value placeof the , ie None.
  • Also, the constructor should include the parent element to add Canvasto ( rootin my example).

Here is a very thorough introduction to all of Tkinter , especially the Canvas element.

Related


How to draw lines on canvas

Arun Kumar I want to draw some lines inside a circle on canvas in the following way. I don't know how to draw lines like below. But I have basic knowledge of drawing lines and arcs on canvas. how to proceed? username You could use a bezier curve with control p

HTML Canvas - how to draw very thin lines?

terminal I created a project using the JavaScript library P5.js and used its built-in line() and ellipse() functions to draw lines and circles on the canvas. I'm now trying to remove the entire P5.js and its functions from my project to keep pure JavaScript bu

How to draw thinner but sharper lines in html canvas?

Evil Guardian I have the following javascript code to draw a graph table. But the problem is when I print the output, the thin lines don't look clear. The problem is visible when zooming the html page. I wish the lines were clearer. But the width should be the

How to draw random/irregular lines in XAML canvas?

Karedia Noorsil I have a canvas in XAML where I want to draw random irregular (hand drawn) lines using C#. This is what I want : http://jsfiddle.net/GfGVE/9/ HTML Canvas but in XAML C# I want the line to be drawn horizontally. Xml: <Canvas Grid.Column="1" x:Na

How to draw only vertical and horizontal lines (canvas)

Worstein Azatian I want to make a drawing tool with html5 canvas, it can only draw horizontal and vertical lines For example, although I will drag the mouse, it has to draw vertical or horizontal lines. Below I will show a picture where I will show what I need

how draw only vertical and horizontal lines (Canvas)

VostanAzatyan I want to make one drawing tool with html5 canvas, that can draw only horizontal and vertical lines For example despite which way I will drag the mouse it must draw vertical or horizontal line. Below I will show one image where i will show what i

How to draw only vertical and horizontal lines (canvas)

Worstein Azatian I want to make a drawing tool with html5 canvas, it can only draw horizontal and vertical lines For example, it has to draw vertical or horizontal lines despite which way I will drag the mouse. Below I will show an image where I will show what

How to draw only vertical and horizontal lines (canvas)

Worstein Azatian I want to make a drawing tool with html5 canvas, it can only draw horizontal and vertical lines For example, it has to draw vertical or horizontal lines despite which way I will drag the mouse. Below I will show an image where I will show what

Draw parallel lines on canvas

Ma Ansi: I am trying to draw parallel lines on a canvas. One of the lines is fixed. The user enters the distance between the two lines, so the second line is positioned as such. I am new to javascript. Please help me how to change the position of the second li

Draw parallel lines on canvas

Ma Ansi: I am trying to draw parallel lines on a canvas. One of the lines is fixed. The user enters the distance between the two lines, so the second line is positioned as such. I am new to javascript. Please help me how to change the position of the second li

Draw multiple lines on the canvas

username I need to draw two lines on the canvas, the first starts from the top and the second starts after 75% of the screen. I try the code below, but when I draw the second line, it affects the first line strokeWidth. How to create two separate lines: void

draw lines on the last canvas

Nikhil I am new to javascript and canvas programming. I have a function drawLines(canvasIndex, startPosition) responsible for drawing lines on the canvas. It accepts two parameters canvasIndex which represents the canvas I want to draw on, and startPosition wh

Draw parallel lines on canvas

Ma Ansi I am trying to draw parallel lines on a canvas. One of the lines has been fixed. The user enters the distance between the two lines, thereby positioning the second line accordingly. I am new to JavaScript. Please help how should I change the position o

Draw parallel lines on canvas

Ma Ansi: I am trying to draw parallel lines on a canvas. One of the lines is fixed. The user enters the distance between the two lines, so the second line is positioned as such. I am new to javascript. Please help me how to change the position of the second li

Draw multiple lines on the canvas

username I need to draw two lines on the canvas, the first starts from the top and the second starts after 75% of the screen. I try the code below, but when I draw the second line, it affects the first line strokeWidth. How to create two separate lines: void

draw lines on the last canvas

Nikhil I am new to javascript and canvas programming. I have a function drawLines(canvasIndex, startPosition) responsible for drawing lines on the canvas. It accepts two parameters canvasIndex which represents the canvas I want to draw on, and startPosition wh

How to draw 1 pixel lines using Javafx Canvas?

Jock Smith: I've been googling and found some related questions/posts, but none solved my problem. I am using the following method to draw lines directly on the canvas (JavaFX): gc.setStroke(color); gc.setLineWidth(lineWidth); gc.strokeLine(startX, startY, end

How to draw 1 pixel lines using Javafx Canvas?

jock smith I've been googling and found some related questions/posts, but none solved my problem. I draw lines directly on the canvas (JavaFX) using: gc.setStroke(color); gc.setLineWidth(lineWidth); gc.strokeLine(startX, startY, endX, endY); I want lines of 1

How to draw sharp curves and diagonal lines on Canvas in Android

pat The image below shows the output on Canvas. Curves and slashes are not as sharp as horizontal or vertical straight lines. Me android:hardwareAccelerated="true"and ANTI_ALIAS_FLAGon Canvasas well. See code below. How can I improve the quality of these curve