본문 바로가기
Python

[Python] Draw shapes using tkinter

by llHoYall 2021. 5. 29.

tkinter is the standard Python interface to the Tk GUI tookit.

In my opinion, I recommend PyQt for GUI application, and tkinter for drawing application.

Most of Python3 include the tkinter package.

Now, let's learn how to draw shapes using tkinter.

Import Module

import tkinter as tk

Create Empty UI

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.canvas = tk.Canvas(
        	self, width=240, height=240, highlightthickness=0
        )
        self.canvas.pack()


app = Application()
app.pack()
app.mainloop()

This is the basic structure of tkinter.

The packer is one of Tk's geometry-management mechanisms. Geometry managers are used to specifying the relative positioning of widgets within their container.

Set Title

Put this code in the constructor with the title that you want.

self.master.title("Empty UI")

Draw Lines

def on_draw_lines(self):
    self.canvas.create_line(20, 20, 100, 20)
    self.canvas.create_line(20, 40, 100, 40, width=1, fill="#d32f2f")
    self.canvas.create_line(20, 60, 100, 60, width=3, fill="#f57c00")
    self.canvas.create_line(20, 80, 100, 80, 
    	width=2, fill="#fbc02d", dash=(5, 2)
    )
    self.canvas.create_line(20, 100, 100, 100, 100, 120, fill="#388e3c")

You can draw lines through create_line() function and configure the width, color, and shape.

If you specify the points serially, the line is drawn continuously.

Draw Circles

def on_draw_circles(self):
    self.canvas.create_oval(140, 20, 160, 40)
    self.canvas.create_oval(180, 20, 220, 40, width=3)
    self.canvas.create_oval(140, 60, 170, 90, fill="#00796b")
    self.canvas.create_oval(190, 60, 220, 90, outline="#1976d2", dash=(5, 2))

You can draw circles through create_oval() function. you need to specify the top-left point and bottom-right point.

Most options are similar to a line.

Draw Rectangles

def on_draw_rectangles(self):
    self.canvas.create_rectangle(20, 140, 40, 170)
    self.canvas.create_rectangle(60, 140, 90, 160, width=2)
    self.canvas.create_rectangle(20, 190, 50, 220, 
    	width=4, fill="#303f9f", outline="#7b1fa2"
    )
    self.canvas.create_rectangle(60, 190, 90, 220, outline="#5d4037", dash=(5, 2))

You can draw rectangles through create_rectangle() function.

The others are the same as create_oval().

Draw Polygons and Arcs

def on_draw_polygons(self):
        self.canvas.create_polygon(
            140, 140, 155, 155, 170, 140, 155, 170,
            width=2, fill="red", outline="blue", dash=(3, 3),
        )
        self.canvas.create_arc(
            190, 140, 220, 170, start=60, extent=120, 
            style="arc",
        )
        self.canvas.create_arc(
            190, 190, 220, 220, start=200, extent=150,
            width=3, fill="green", outline="blue", dash=(5, 2),
        )

You can draw polygons through create_polygons() function. You need to specify all the points to draw.

In addition, you can draw arcs through create_arc() function. You need to specify the start angle and the angle of the arc.

Draw Images

For handling images on Python, we need the Pillow module.

$ pip install Pillow
def on_draw_images(self):
    self.img = ImageTk.PhotoImage(Image.open("test_image.jpg"))
    self.canvas.create_image(140, 190, image=self.img, anchor=tk.NW)

You can draw images with the above code.

The anchor means north-west, i.e. top-left.

Draw Texts

def on_draw_texts(self):
    self.canvas.create_text(
    	20, 110, text="Hello tkinter", font="consolas 12", anchor=tk.NW
    )

You can draw texts through create_text() function.

Conclusion

In this posting, we can figure out how to draw shapes using tkinter.

The whole example code is below.

It is so easy that you can understand it just by looking at it.

import tkinter as tk
from PIL import Image, ImageTk


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)

        self.master.title("Draw Shapes")

        self.canvas = tk.Canvas(
        	self, width=240, height=240, highlightthickness=0
        )
        self.canvas.pack()

        self.canvas.delete("all")

        self.on_draw_lines()
        self.on_draw_circles()
        self.on_draw_rectangles()
        self.on_draw_polygons()
        self.on_draw_images()
        self.on_draw_texts()

    def on_draw_lines(self):
        self.canvas.create_line(20, 20, 100, 20)
        self.canvas.create_line(20, 40, 100, 40, width=1, fill="#d32f2f")
        self.canvas.create_line(20, 60, 100, 60, width=3, fill="#f57c00")
        self.canvas.create_line(20, 80, 100, 80, 
        	width=2, fill="#fbc02d", dash=(5, 2)
        )
        self.canvas.create_line(20, 100, 100, 100, 100, 120, fill="#388e3c")

    def on_draw_circles(self):
        self.canvas.create_oval(140, 20, 160, 40)
        self.canvas.create_oval(180, 20, 220, 40, width=3)
        self.canvas.create_oval(140, 60, 170, 90, fill="#00796b")
        self.canvas.create_oval(190, 60, 220, 90, outline="#1976d2", dash=(5, 2))

    def on_draw_rectangles(self):
        self.canvas.create_rectangle(20, 140, 40, 170)
        self.canvas.create_rectangle(60, 140, 90, 160, width=2)
        self.canvas.create_rectangle(
            20, 190, 50, 220, width=4, fill="#303f9f", outline="#7b1fa2"
        )
        self.canvas.create_rectangle(
        	60, 190, 90, 220, outline="#5d4037", dash=(5, 2)
        )

    def on_draw_polygons(self):
        self.canvas.create_polygon(
            140, 140, 155, 155, 170, 140, 155, 170,
            width=2, fill="red", outline="blue", dash=(3, 3),
        )
        self.canvas.create_arc(
            190, 140, 220, 170, start=60, extent=120, style="arc",
        )
        self.canvas.create_arc(
            190, 190, 220, 220, start=200, extent=150,
            width=3, fill="green", outline="blue", dash=(5, 2),
        )

    def on_draw_images(self):
        self.img = ImageTk.PhotoImage(Image.open("test_image.jpg"))
        self.canvas.create_image(140, 190, image=self.img, anchor=tk.NW)

    def on_draw_texts(self):
        self.canvas.create_text(
            20, 110, text="Hello tkinter", font="consolas 12", anchor=tk.NW
        )


app = Application()
app.pack()
app.mainloop()

'Python' 카테고리의 다른 글

[Python3] typing module  (0) 2021.07.03
[Python] Decorator  (0) 2021.06.29
[Python] itertools module  (0) 2021.05.15
[Python] functools module  (0) 2021.05.08
[Python] ChainMap in collections  (0) 2021.05.02

댓글