Sunday, January 27, 2019

tkinter examples - 4 (Canvas)

On canvas widget we can draw figures like lines,circle,rectangles etc. We can also draw text, images and other widgets on canvas. To draw shapes in the canvas, use the create methods to add new items. Let's create a canvas and draw some shapes on it.

1. rectangle: For creating rectangles we have the method create_rectangle(coords, options). Coords is again defined by two points, but this time the first one is the top left point and the bottom right point of the rectangle.

See the program below:

from tkinter import *

root = Tk()

canvas = Canvas (root,width = 250, height = 250, bg = 'green')

canvas.create_rectangle(202,100,30,150, fill = 'red')

canvas.pack()

mainloop()

We first created a canvas with a green background that is 250* 250 pixels in size as shown in the following code:

canvas = Canvas (root, width = 250, height = 250, bg = 'green')

Next we draw a rectangle filled with red color as shown by the following code:

canvas.create_rectangle(202,100,30,150, fill = 'red')

The upper left of the rectangle is at (202, 100), and the lower right is at (30, 150).




If were to leave off  bg = 'green and 'fill='red' fields , the result would be a canvas with white background with a rectangle having black outline as shown in the figure below: 




2. Lines: The method create_line(coords, options) is used to draw a straight line. The coordinates "coords" are given as four integer numbers: x1, y1, x2, y2 This means that the line goes from the point (x1, y1) to the point (x2, y2) After these coordinates follows a comma separated list of additional parameters, which may be empty.

3. Ovals: We can create an oval on a canvas c with the following method:

create_oval ( x0, y0, x1, y1, option, ... )


We can draw lines and ovals in the similar way as we drew rectangle. The following program draws ovals and lines:

from tkinter import *

root = Tk()

canvas = Canvas (root,width = 250, height = 250)

canvas.create_rectangle(202,100,30,150)
canvas.create_oval(202,100,30,150, fill = 'green')
canvas.create_line(202,100,30,150, fill = 'red')

canvas.pack()

mainloop()

The output of the program is shown below:



4. Polygons: To draw a polygon we use create_polygon method() and provide at least three coordinate points in the create_polygon method:

create_polygon(x0,y0, x1,y1, x2,y2, ...)

The following program draws a triangle:

from tkinter import *

root = Tk()

canvas = Canvas (root,width = 220, height = 200)

canvas.create_polygon(0,0,200,100,0,200, outline="#476042",fill='yellow', width=3)

canvas.pack()

mainloop()

The output of the program is shown below:



5. Images: The Canvas method create_image(x0,y0, options ...) is used to draw an image on a canvas. create_image doesn't accept an image directly. It uses an object which is created by the PhotoImage() method. The PhotoImage class can only read GIF and PGM/PPM images from files. See the program below:

from tkinter import *

root = Tk()

canvas = Canvas (root,width = 220, height = 200)

canvas.pack()

img = PhotoImage(file="covri.gif")
canvas.create_image(20,20, anchor=NW, image=img)

mainloop()

The output of the program is shown below:



6. Bitmaps: The method create_bitmap() can be be used to include a bitmap on a canvas. The following bitmaps are available on all platforms:

"error", "gray75", "gray50", "gray25", "gray12", "hourglass", "info", "questhead", "question", "warning"

The following program puts all of these bitmaps on a canvas:

from tkinter import *

root = Tk()

canvas = Canvas (root,width = 300, height = 100)

canvas.pack()

bitmaps = ["error", "gray75", "gray50", "gray25", "gray12", "hourglass", "info", "questhead", "question", "warning"]
nsteps = len(bitmaps)
step_x = int(300 / nsteps)

for i in range(0, nsteps):
   canvas.create_bitmap((i+1)*step_x - step_x/2,50, bitmap=bitmaps[i])

mainloop()

The output of the program is shown below:






Here I am ending today's post. Make some programs on your own as GUI needs a lot practice. Till we meet next keep practicing and learning Python as Python is easy to learn!
Share:

0 comments:

Post a Comment