Python Exercise Example 58
**Problem:** Draw a picture; learn to use rectangle to draw rectangles.
Program analysis:
rectangle(int left, int top, int right, int bottom)
Parameter description: (left, top) are the upper-left coordinates of the rectangle, (right, bottom) are the lower-right coordinates; together they determine the size of a rectangle
Example (Python 2.0+)
#!/usr/bin/python
-- coding: UTF-8 --
if
name
==
'
main
'
:
from
Tkinter
import
root
=
Tk
(
)
root
.
title
(
'
Canvas
'
)
canvas
=
Canvas
(
root
,
width
=
400
,
height
=
400
,
bg
=
'
yellow
'
)
x0
=
263
y0
=
263
y1
=
275
x1
=
275
for
i
in
range
(
19
)
:
canvas
.
create_rectangle
(
x0
,
y0
,
x1
,
y1
)
x0
-=
5
y0
-=
5
x1
+=
5
y1
+=
5
canvas
.
pack
(
)
root
.
mainloop
(
)
The output of the above example is:
Python 100 Examples