Tutorial #68: Creating Lines and Shapes with Canvas in Python Tkinter

Posted by


In this tutorial, we will learn how to draw lines and shapes using the Canvas widget in a Python Tkinter GUI application. The Canvas widget is a powerful tool that allows us to create interactive graphics in our GUI applications.

To get started, make sure you have Python installed on your system. You can download the latest version of Python from the official website (https://www.python.org/downloads/). Once you have Python installed, you can install Tkinter by running the following command in your terminal:

pip install tk

Now that we have Tkinter installed, we can start creating our GUI application. First, let’s create a new Python file and import the necessary modules:

import tkinter as tk

Next, let’s create a new Tkinter window and add a Canvas widget to it:

root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()

Now that we have our Canvas widget set up, let’s start drawing some lines and shapes on it. To draw a line on the Canvas, we can use the create_line() method. The create_line() method takes four arguments: the x and y coordinates of the starting point, and the x and y coordinates of the ending point. We can use this method to draw a simple line like this:

line = canvas.create_line(100, 100, 300, 300)

This will draw a line from the point (100, 100) to the point (300, 300) on the Canvas. You can customize the appearance of the line by passing additional arguments to the create_line() method, such as fill (to set the color of the line) and width (to set the width of the line).

Next, let’s draw some shapes on the Canvas. To draw a rectangle on the Canvas, we can use the create_rectangle() method. The create_rectangle() method takes four arguments: the x and y coordinates of the top-left corner, and the x and y coordinates of the bottom-right corner. We can use this method to draw a rectangle like this:

rectangle = canvas.create_rectangle(50, 50, 150, 150, fill='blue')

This will draw a blue rectangle with the top-left corner at (50, 50) and the bottom-right corner at (150, 150) on the Canvas.

We can also draw other shapes on the Canvas, such as circles, ovals, polygons, and arcs. To draw a circle on the Canvas, we can use the create_oval() method. The create_oval() method takes four arguments: the x and y coordinates of the top-left corner of the bounding box, and the x and y coordinates of the bottom-right corner of the bounding box. We can use this method to draw a circle like this:

circle = canvas.create_oval(200, 50, 300, 150, fill='red')

This will draw a red circle that fits inside the bounding box with the top-left corner at (200, 50) and the bottom-right corner at (300, 150) on the Canvas.

You can experiment with different shapes and colors by creating multiple lines and shapes on the Canvas. Once you have finished drawing lines and shapes on the Canvas, you can run the Tkinter main loop to display the GUI application:

root.mainloop()

That’s it! You have successfully learned how to draw lines and shapes using the Canvas widget in a Python Tkinter GUI application. I hope you found this tutorial helpful. Feel free to experiment with different shapes and colors to create your own interactive graphics in Tkinter. Happy coding!

0 0 votes
Article Rating

Leave a Reply

37 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Codemycom
2 hours ago

▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My YouTube Channel:
http://bit.ly/2UFLKgj http://bit.ly/2IGzvOR
▶️ See More At: ✅ Join My Facebook Group:
https://Codemy.com http://bit.ly/2GFmOBz
▶️ Learn to Code at https://Codemy.com ✅ Buy a Codemy T-Shirt!
Take $30 off with coupon code: youtube1 http://bit.ly/2VC9WUN

@quaterman1270
2 hours ago

x coordinate starts on the left side and the y coordinates start at the top. Your x was correct but your y coordinate needs to sart at the top. Top is 0 and bottom is 200

@Zellos376
2 hours ago

Thank you very much

@kapibara2440
2 hours ago

A very interesting intro to Canvas!

@somerandomgadzo4778
2 hours ago

Bro is Walter White.

"I am the one who CODES"

@adammax9902
2 hours ago

shouldn't the rectangle be closer to the bottom

@eutral
2 hours ago

This is indeed a helpful tutorial, but you have flipped the Y-axis. Y-position is 0 at the top and increases as it goes towards the bottom

@lemonsquezy11
2 hours ago

TYSM!!!

@geetachavan2916
2 hours ago

this is so much helpful

@imanqoly
2 hours ago

Thanks a lot for your great content, since you explained on another video that a UI is a loop that is constantly rendering, is it possible to draw a shape shifting rectangle with dynamic coordinations?

@paulpaul6042
2 hours ago

Hey Is that possible to put inside that canvas 2 random points (X) distance to a 1point moving (Y).. like Y moving inside randomly and we see distance to X points

@JoeMamaJunk
2 hours ago

Can you make a video on how to program a bucket/flood fill function that can be used on Tkinter canvases?

@Mark00747
2 hours ago

Could you let the user create custom lines with the cursor?

@solybr2271
2 hours ago

Hi, in my project i have to do it on video stream. Do you have any idea how can I do it please?

@elysonpanolino5413
2 hours ago

how do i put text in tkinter canvas

@jokarjapan
2 hours ago

clear explains, thank you

@asmeergillani2991
2 hours ago

Can you please explain why you wrote my_canvas.create_rectangle

@abhinavdubey8077
2 hours ago

very informative video ..

@brauliogarcia1836
2 hours ago

I like the way you teach, thank you for sharing your knowledge!

@funnyclips2696
2 hours ago

It's just awesome! Even though my background is Urban planning and GIS, I always learned from your simplified tutorials. 👌🏿

37
0
Would love your thoughts, please comment.x
()
x