Python, part 8
Tk graphics, continued.
Polygons. A polygon is a closed shape--which a line or a curve is
not. You
can change the color inside a polygon, whereas a simple line has no
inherent
inside and outside. Polygons are specified by 3 or more points,
meaning 3 or
more pairs of (x,y) coordinates. We can have degenerate polygons where all
the points are on a straight line--we no longer have a real inside, and
all we
have is a line and an outside.
myCanvas.create_polygon(10, 15, 75, 100, 50, 50) tells Tk that
we have
3 points: (10,15), (75,100), and (50,50). If you want
something with 12 points
you'd need 12 pairs of coordinates. fill, outline, width are
available as before.
TEXT in graphics. myCanvas.create_text(100, 100, "Go
Vols!") would put
the specified text horizontally, centered on the point (100,100)--NOT
beginning
on that point. Options for text include font which lets you pick the
font style
and size (e.g. font=("courier", 8)) and also anchor which lets you specify the
point of the bounding box--n,nw,e, se, s, sw,w, nw--north, northwest,
etc. nw
is the upper left-hand corner of the box.
RANDOM NUMBERS in Python. These can be fun--we'll see shortly how
we
might combine these with Tk graphics.
import
random
this brings in the library
number = random.randrange(1,
100) generates a random number between
1 and 99 (one less than the max 100
we specified)
DiceRoll = random.randrange(1, 7)
+ random.randrange(1, 7)
this gives you a two-dice total from 2 to 12
RANDOM LINES in Tk
Let's use Tk to draw some connected lines at random. We'll start
at (0,0)--
the upper left-hand corner, and draw a line to the random coordinates
(x1,y1)
where x1 and y1 are between 1 and 400. We'll then draw a line
from (x1,y1) to
(x2,y2), also picked at random. We'll do 4 lines this way, and
we'll have arrows
at the far end of the lines. To do exactly 4 lines, we can
use a for loop. In the
middle of the process we'll be wanting to draw a line from (oldx,oldy)
to
(newx,newy):
newx =
random.randrange(1, 401)
newy =
random.randrange(1, 401)
myCanvas
.create_line(oldx, oldy, newx, newy, arrow="second")
This draws the new line. But how to get the next line?
oldx = newx
oldy = newy
and then we repeat the 3 lines above. Not too hard! So:
oldx = 0
oldy =
0
this starts the process off the way we want
for i in range(4):
newx = random.randrange(1, 401)
newy = random.randrange(1, 401)
myCanvas.create_line(oldx, oldy, newx, newy, arrow="last")
oldx = newx
oldy = newy
forlines
Remember that this goes into the Tk code--and remember to also have
import random--put it before the Tk import stuff. Try the
code. Run
it again! What has happened? The lines go to different
points--the x-y
coordinates are picked at random--so this should not be a surprise.
We can try a variation with a while loop--recalling that you need some
way to break out of the loop! We might keep drawing new lines
until
x > 350, for example. Don't go overboard here with x >
395--you might
get an awful lot of lines!
oldx = 0
oldy = 0
while(oldx <= 350):
newx = random.randrange(1,401)
newy = random.randrange(1,401)
myCanvas.create_line(oldx,oldy,newx,newy,arrow="last")
oldx = newx
oldy = newy
randwhilelines
Notice that the last/final arrow has the arrowhead near the right of our
canvas--which is what we expect, if you think about it.
We can also use the random-number generator to pick colors at random
for us:
WhichColor = random.randrange(1,6)
if WhichColor == 1:
myColor = "purple"
elif WhichColor == 2:
myColor = "green"
elif WhichColor == 3:
myColor = "red"
elif WhichColor == 4:
myColor = "blue"
else:
myColor = "yellow"
And then when you draw a rectangle (or whatever) you can have as
one of the parameters fill=myColor