Hit enter after type your search item

Canvas in Python tkinter

What is canvas in tkinter?

In tkinter, the canvas widget is used to draw graphics.

You can draw:

  • Text
  • Widgets
  • Graph and plots
  • Frames

A tkinter canvas is a rectangular area.

See the examples below for drawing various elements in the canvas widget.

A simple example of creating a canvas

In the first example, we just created a canvas widget without any graphics to show how it looks.

Code:


Output:

tkinter-canvas-simple

Items that can be created in canvas

Tkinter canvas supports following items:

  • Line
  • Arc
  • Oval
  • Text
  • Image
  • Rectangle

See the examples below of creating these items with their respective methods.

Creating a line in canvas example

For creating a line in the canvas, you may use the create_line() method. See an example below where we created a line in the canvas in yellow color:

Code:


Output:

tkinter-canvas-line

Creating an arc in canvas example

Use create_arc() method for creating an arc in the canvas.

In the program below, we created an arc by providing arc coordinates.

Code:


Output:

tkinter-canvas-arc

Creating an oval shape example

The syntax for creating an oval is:

create_oval(*args, **kw)

Where arg is two coordinate points.

See an example below where we created a shape like a chicken egg:

Code:


Output:

tkinter-canvas-oval

An example of polygon in canvas

In the example below, we created a polygon by specifying its points.

Code:


Output:

tkinter-canvas-polygon

Creating a text entry in canvas example

The following program creates an input entry box inside the canvas.

Python program:


Output:

tkinter-canvas-entry

For detailed options and features available with canvas in tkinter, visit this documentation:

https://tkinter-docs.readthedocs.io/en/latest/widgets/canvas.html

This div height required for enabling the sticky sidebar