Python Tkinter Canvas

Created with Sketch.

Python Tkinter Canvas

The canvas widget is used to add the structured graphics to the python application. It is used to draw the graph and plots to the python application. The syntax to use the canvas is given below.

Syntax

  1. w = canvas(parent, options)

A list of possible options is given below.

SNOptionDescription
1bdThe represents the border width. The default width is 2.
2bgIt represents the background color of the canvas.
3confineIt is set to make the canvas unscrollable outside the scroll region.
4cursorThe cursor is used as the arrow, circle, dot, etc. on the canvas.
5heightIt represents the size of the canvas in the vertical direction.
6highlightcolorIt represents the highlight color when the widget is focused.
7reliefIt represents the type of the border. The possible values are SUNKEN, RAISED, GROOVE, and RIDGE.
8scrollregionIt represents the coordinates specified as the tuple containing the area of the canvas.
9widthIt represents the width of the canvas.
10xscrollincrementIf it is set to a positive value. The canvas is placed only to the multiple of this value.
11xscrollcommandIf the canvas is scrollable, this attribute should be the .set() method of the horizontal scrollbar.
12yscrollincrementWorks like xscrollincrement, but governs vertical movement.
13yscrollcommandIf the canvas is scrollable, this attribute should be the .set() method of the vertical scrollbar.

Example

  1. from tkinter import *
  2. top = Tk()
  3. top.geometry(“200×200”)
  4. #creating a simple canvas
  5. c = Canvas(top,bg = “pink”,height = “200”)
  6. c.pack()
  7. top.mainloop()

Output:

Python Tkinter Canvas

Example: Creating an arc

  1. from tkinter import *
  2. top = Tk()
  3. top.geometry(“200×200”)
  4. #creating a simple canvas
  5. c = Canvas(top,bg = “pink”,height = “200”,width = 200)
  6. arc = c.create_arc((5,10,150,200),start = 0,extent = 150, fill= “white”)
  7. c.pack()
  8. top.mainloop()

Output:

Python Tkinter Canvas

 

Leave a Reply

Your email address will not be published. Required fields are marked *