Drawing Basic Shapes and Lines with Python GUI - tkinter

In this article, we will explore how to draw basic shapes and lines using the Python GUI library tkinter. Tkinter provides a simple and intuitive way to create graphical user interfaces, and drawing shapes and lines is an essential part of any GUI application.

Getting Started

Before we dive into the drawing functions provided by tkinter, let's make sure we have tkinter installed on our system. You can check if tkinter is installed by running the following command in your terminal:

python -m tkinter

If the installation is successful, the tkinter window should open without any errors.

Creating a Canvas

To begin drawing the shapes and lines, we need to create a canvas widget. The canvas acts as a drawing board where we can place and manipulate various graphical elements. The canvas can be created using the Canvas class from the tkinter library:

import tkinter as tk

# Create the main window
root = tk.Tk()

# Create a canvas
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()

# Run the tkinter main loop
root.mainloop()

In the above code, we create a tkinter window and a canvas with a width of 400 pixels and a height of 400 pixels. The pack() method packs the canvas into the main window. Finally, the mainloop() method runs the tkinter event loop, which keeps the window open and responsive.

Drawing Shapes

Tkinter provides various methods to draw shapes on the canvas. Let's explore a few of them:

Rectangle

To draw a rectangle on the canvas, we can use the create_rectangle method. This method takes four parameters: the x and y coordinates of the top-left corner of the rectangle and the x and y coordinates of the bottom-right corner of the rectangle.

# Draw a rectangle
rectangle = canvas.create_rectangle(50, 50, 150, 100, fill="red")

The above code creates a rectangle with a top-left corner at (50, 50) and a bottom-right corner at (150, 100). The rectangle is filled with the color red.

Circle

We can draw a circle on the canvas using the create_oval method. This method takes four parameters: the x and y coordinates of the top-left bounding box of the oval and the x and y coordinates of the bottom-right bounding box of the oval.

# Draw a circle
circle = canvas.create_oval(200, 50, 300, 150, outline="blue", width=2)

The above code creates a circle with a top-left bounding box at (200, 50) and a bottom-right bounding box at (300, 150). The circle has a blue outline with a width of 2 pixels.

Line

To draw a line on the canvas, we can use the create_line method. This method takes four parameters: the x and y coordinates of the starting point of the line and the x and y coordinates of the ending point of the line.

# Draw a line
line = canvas.create_line(50, 150, 150, 250, fill="green", width=3)

The above code creates a line from the point (50, 150) to the point (150, 250). The line is filled with the color green and has a width of 3 pixels.

Modifying Shapes and Lines

Once created, we can modify the shapes and lines on the canvas using various methods provided by tkinter.

Moving Shapes

To move a shape on the canvas, we can use the move method. This method takes three parameters: the shape object to move and the horizontal and vertical distances by which to move the shape.

# Move the rectangle
canvas.move(rectangle, 50, 0)

The above code moves the rectangle shape by 50 pixels horizontally and 0 pixels vertically.

Deleting Shapes

To delete a shape from the canvas, we can use the delete method. This method takes the shape object to delete as a parameter.

# Delete the circle
canvas.delete(circle)

The above code deletes the circle shape from the canvas.

Conclusion

In this article, we have explored how to draw basic shapes and lines using the Python GUI library tkinter. We learned how to create a canvas, draw shapes and lines, and modify them on the canvas. With these techniques, you can create visually appealing graphical user interfaces in Python. Have fun experimenting with different shapes and lines on your canvas!


noob to master © copyleft