Rating: 1 Star2 Stars3 Stars4 Stars5 Stars

Freeform GUIs in Python: Tkinter canvas

The Tkinter framework provides some standard GUI widgets for use in building Graphical User Interfaces in Python. If you need more freedom you can use the Canvas widget included in Tkinter, which gives you an area where you can draw custom shapes.

canada goose parka expedition femme, Canada Goose coats, Canada Goose sale, Canada Goose Factory Outlet,canada goose mens tundra., Canada Goose Outlet, Canada Goose, Canada Goose

We’ll start with a basic Tk application. This will create a window containing a Canvas widget 300 pixels by 300. Note: you can type all the commands in this article into your interpreter, and the GUI window will be updated as you go.

The resulting application doesn’t look like much, you should have a square, grey window. More interesting is to draw shapes to the canvas — the follow code, using the create_rectangle method of the canvas, draws a green square to the top-left corner:

create_rectangle takes four positional arguments representing the top-left and bottom-right coordinates of the rectange, and then a list of optional named parameters. In this case we set the fill colour to green. For a full list, you can browse the documentation for Tkinter rectangles here.

Likewise, you can draw other shapes to the canvas. The create_oval canvas method works in the same way as the create_rectangle method, except that it draws the ellipse contained within the bounding rectangle. The following line draws a blue circle directly below the square:

The next basic shape type to learn is the polygon, which allows you to draw objects of any shape. The create_polygon method takes in any number of positions and draws the shape that is formed by using all the positions as vertices. The following example draws a red diamond to the right of the square:

Polygons could have any amount of points, so you could have just as easily drawn a five, six or seven sided shape here, rather than a diamond.

Lastly you can add text to the canvas by using the create_text method. This method takes the centre point of the text object, then optional arguments including the colour. The most important of these arguments is text which is the text to be written to the canvas. In the following example we write some simple text to the bottom-right corner of the window:

We also manually set the font, to make the text appear larger. At their simplest, font’s are simply a tuple containing a font name and size — in this case, 16 point Helvectica.

Deleting items from the canvas

You may have noticed that we’ve been storing the value returned by the create methods used to add shapes to the canvas. Each creation method returns an object indentifier, which allows us to manipulate the objects after they have been added.

You can delete any item from the canvas by using canvas.delete:

By adding and removing items from the canvas, you can create more sophisticated and customised feedback to users of your applications. The canvas is highly customisable and allows more complicated interactions with objects.

Leave me a comment and let me hear your opinion. If you’ve got any thoughts, comments or suggestions for things we could add, leave a comment! Also please Subscribe to our RSS for latest tips, tricks and examples on cutting edge stuff.

Related Posts

How to Write and append to zip archives in Python

How to read zip archives in Python

Treat Google as an application platform

Python Web Scrapper with BeautifulSoup