Last Updated:

Drawing in Python TKinter

Drawing in Python TKinter

TKinter – a software that allows you to literally draw different shapes using commands. To perform such actions in the algorithm, Python is needed, since the package for creating graphics is written for such a programming language. You can work with graphical components if you install the Canvas widget. You can create a variety of elements. It can be any simple figure with a certain color, text with a small picture, and with due ingenuity - a widget or a simple game.

At the disposal of the user is a lot of all kinds of tools, including both primitive to add code, and complex. However, there are even situations when it is difficult to draw a simple dot with the Canvas add-on. Therefore, sometimes it is necessary to approach the task with logic and think about how to perform this or that action. After all, sometimes there can be no standard ways to install certain elements on the workspace in TKinter.

Users can also use not only simple shapes with black strokes, but also apply various settings to them. In particular, this is a color, because the size, of course, a person himself will understand that it is possible to change. But there is another element – the text. It can change the font with the same color. Therefore, it is possible to do anything, using primitive graphics tools, as well as customizing them on the working area with the help of code.

Drawing points

A point is the simplest unit that the user creates in simple graphics editors with a single click on a coordinate. It is, in fact, a single-pixel element of any type. That is, it is possible not only to simply click on the working plane using the "brush" / "pencil" tool, but also to work with the dimensions. However, when using the creation of shapes, you will have to try, because a person will repeatedly reduce the oval, rectangle and other shapes that you want to turn into a point. But more often than not, it will be a pointless exercise.

But not in Python – TKinter does not have a default command that creates any point, which is why you will have to approach the task with a certain amount of ingenuity. It is possible, again, to use shapes, but it will be very difficult to do, because you will have to adjust many parameters to reduce to a pixel. Even if you work with a square, you need to set a fine border, and then reduce, decrease and decrease the object. Not very convenient. It is best to use a line, which is a primitive object when working with Canvas.

Why the line? The answer is simple. It can be easily turned into a point by superimposing both ends on the same coordinate. So you will get the desired painted pixel (or several of them, forming a small "spot"). Therefore, a person will see a point, but in fact, it will be a line. How to do this is shown with a single line of code:

canvas.create_line(x, y, x+1, y, fill=”#ff0000”)

Instead of text specifying a value of "fill", you can enter any text that contains color code. Thus, a person will paint the dot in the chosen shade. In this case, a simple red is used - without impurities of others. If you want something "more interesting" - you should find the color codes, and then look for a suitable parameter for yourself.

You can apply a dot in this way, for example, in a caricature or for some small objects like buttons in a man. To come up with any application , you just need to think. And this, in this case, is a five-minute affair - if desired, you can even "invent" something new, using a point when drawing in TKinter.

Drawing text


When creating text in TKinter using the Canvas add-on, you need to additionally know that a coordinate system is used to position it on the work surface. Users will have to work with two indicators of two-dimensional space. The first is x, which is responsible for the horizontal direction, and the second is y, the value of which is determined by the indentation from above.

It is possible to prescribe the same raster code without much effort - it is required for this only to use the sample provided below. It does not contain any complex structures, which is why it is possible to easily understand its structure. Here, directly, he himself:

from tkinterimport Tk, Canvas, Frame, BOTH, W

class Example(Frame):
def __init__(self):

def initUI(self):
self.master.title(Text and Font in Tkinter")
self.pack(fill=BOTH, expand=1)

canvas = Canvas(self)
20, 30, anchor=W, font="DejavuSansLight",


With the help of such a set of commands, a person can write in the Canvas widget whatever he wants. However, you need to understand that you need to set your own parameters. The analysis of them is very simple. That is, a person will be able to create any inscription if he knows that:

Under the inscription "canvas.create_text (" first write the x coordinate (indentation to the left), and also, after the comma - the point y (indent from above). "anchor=W" - where the text begins, it is better to leave such a value for convenience and familiarity of the result. The penultimate parameter that depends on the user is "font="any font that a person needs"", where, of course, the font with which the inscription will be written is set. And at the bottom, as you can guess, is the very phrase that needs to be deduced.

As you can see, it is very easy to write text. And this procedure takes quite a bit of time. In fact, it is possible to experiment with the locations of inscriptions, as well as fonts, directions and other parameters. In general, everything basic is available to the user and he can easily write various inscriptions in canvas.

Drawing simple graphics with Python 3(Turtle)


It is also possible to draw various shapes using a "turtle". Such a tool is used for a variety of images. The main thing is to be able to manage this function and know what you want to draw. And to do this is quite simple - work with a turtle (Turtle) occurs directly through the teams. Standard look and will not give reason to ask a lot of questions about the structure:

import turtle


Thus, it is possible to move the "turtle" first turning to the right by 90 degrees, then passing 100 pixels forward (on its side), make a turn to the left by 90 degrees, and at the end go 100 pixels (again, if you look at it "with your eyes"). However, it should be understood that not a turtle will appear when performing the program, but a simple arrow that moves. This can be fixed with a simple command, which is presented below:

import turtle


Now, instead of an arrow, a person will have a turtle, albeit black without individual details. Also, it is possible to use various useful abbreviations for directions: rt(right), fd(forward), lt(left), bk(backward). And so you can make the process of writing code faster. However, it is required to understand that such phrases will not be able to achieve great success for the sake of this and such phrases will be executed in the same way as full-size ones.

But in any case, in Python, with the help of Turtle commands, it is possible to draw whatever you want. This will be a good addition to the main functionality of TKinter. The main thing in drawing in Python is knowledge of the basics and creativity.