Draw a rectangle

Draw a basic rectangle by specifying its position, shape, and color attributes as named variables.

How to do it...

The instructions used in recipe 1 should be used.

Just use the name rectangle.py when you write, save, and execute this program.

# rectangle.py
#>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title('Basic Rectangle')
cw = 200 # canvas width
ch =130 # canvas height
canvas_1 = Canvas(root, width=cw, height=200, background="white")
canvas_1.grid(row=0, column=1)
x_start = 10
y_start = 30
x_width =70
y_height = 90
kula ="darkblue"
canvas_1.create_rectangle( x_start, y_start,
x_start + x_width, y_start + y_height, fill=kula)
root.mainloop()
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How it works...

The results are given in the next screenshot showing a basic rectangle.

How it works...

When drawing rectangles, circles, ellipses and arcs you specify the start point (the bottom-left corner) and then the end point (top-right corner) of the bounding box surrounding the figure being drawn. In the case of rectangles and squares, the bounding box coincides with the figure. But in the case of circles, ellipses, and arcs the bounding box is of course larger.

With this recipe we have tried a new way of defining the shape of the rectangle. We give the start point as [x_start, y_start] and then we just state the width and height that we want as [x_width, y_height]. This way the end point is [x_start + x_width, y_start + y_height]. This way you only need to state what the new start point is if you want to create a multiplicity of rectangles having the same height and width.

There's more...

In the next example, we use a common shape to draw a series of similar but different rectangles.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161