Saturday, July 27, 2024
HomejQueryBe taught to make use of Canvas in Python tkinter

Be taught to make use of Canvas in Python tkinter


What’s canvas in tkinter?

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

You may draw:

  • Textual content
  • Widgets
  • Graph and plots
  • Frames

A tkinter canvas is an oblong space.

See the examples under for drawing varied parts within the canvas widget.

A easy instance of making a canvas

Within the first instance, we simply created a canvas widget with none graphics to point out the way it seems.

Code:



Output:

tkinter-canvas-simple

Objects that may be created in canvas

Tkinter canvas helps following gadgets:

  • Line
  • Arc
  • Oval
  • Textual content
  • Picture
  • Rectangle

See the examples under of making this stuff with their respective strategies.

Making a line in canvas instance

For making a line within the canvas, chances are you’ll use the create_line() technique. See an instance under the place we created a line within the canvas in yellow colour:

Code:



Output:

tkinter-canvas-line

Creating an arc in canvas instance

Use create_arc() technique for creating an arc within the canvas.

In this system under, we created an arc by offering arc coordinates.

Code:



Output:

Creating an oval form instance

The syntax for creating an oval is:

create_oval(*args, **kw)

The place arg is 2 coordinate factors.

See an instance under the place we created a form like a rooster egg:

Code:



Output:

tkinter-canvas-oval

An instance of polygon in canvas

Within the instance under, we created a polygon by specifying its factors.

Code:



Output:

tkinter-canvas-polygon

Making a textual content entry in canvas instance

The next program creates an enter entry field contained in the canvas.

Python program:



Output:

tkinter-canvas-entry

For detailed choices and options out there with canvas in tkinter, go to this documentation:

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


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments