Python, part 7
GRAPHICS. Also see Mike
Orsega's pages.
In Python, as with most
programming languages, you can make use of
what are called libraries.
Libraries usually contain material that is not
an inherent part of the main language, but which are needed for various
applications. Why not make everything inherent? To do so
would make
things much more complex, and the language footprint (i.e. how much
memory the compiler/interpreter takes up) would be much larger, and
compilation/interpretation would probably be slower. Think of your
car: it came with a basic package that probably included air
conditioning.
If you had bought it in Nome, Alaska, would the package have included
the same features? Probably not. Did your car come with a
heavy-duty
trailer hitch? With bulletproof glass windows? With a
1000-watt stereo
system? White with big orange spots? You can add such
things on if you
want those options.
Libraries are brough in by using the
import feature. For example:
import random
would go up at the top of your source code--this would bring in, as
we'll
see later, a random-number generator--we'll be looking at how you can
play with this. The Tk graphics package in Python is brought into
your
source code in a rather similar way, although some features will be very
strange-looking:
from Tkinter
import
*
this imports the Tk interface to your
source code.
To draw some pictures, we'll also need to tell the Tk interface a few
things.
We'll assume that we have a canvas, so to speak, to draw our stuff
on--we
need to give the canvas a name, so we can call it myCanvas. We might
note here that Tk has some reserved words--including the word Canvas, so
if having a reserved word Canvas, and your variable myCanvas bothers
you,
you might call your variable myPicture or whatever you like. So
here we go:
from Tkinter import *
frmMain =
Tk()
this gets us to the Tk interface
myCanvas = Canvas(width=400, height=400,
background="orange")
this tells the Tk GUI that your variable myCanvas will
be a pallette that is 400 x 400, painted orange to start with
you'll be
telling Tk here what shapes you want
myCanvas.pack()
you've been learning in class about packed binary
frmMain.mainloop()
this closes things off from the Tk GUI
This gets us a 400 x 400 canvas we can play with. Each point on
this canvas is
specified by a PAIR of (x,y) coordinates--just like in math
class. It's easy--
no reason to panic. Coordinates (0,0) is the upper left-hand
corner of our
canvas. Coordinate (150,20) would mean a spot that is 150 pixels
from the left
side and 20 units down from the top of the canvas. (400,400)
would be the lower
right corner.
We can draw various shapes on myCanvas--remember you specify your
variable
here:
myCanvas.create_rectangle(10, 15, 75, 100)
this tells Tk to create--in your variable myCanvas--a rectangle.
To specify where
the rectangle is, we need to specify the upper left-hand corner (10,
15) and the lower
right-hand corner at (75, 100). You have said that you want to
create a rectangle,
so you shouldn't worry about Tk creating a cricle or something
instead. The create
here (there will also be create_oval, etc) is a function that has some
additional options.
fill="purple" tells Tk
to fill in the rectangle in purple
outline="yellow"
tells Tk you want your outline to be yellow rather than black
width=5
tells Tk how wide the outline line
should be
myCanvas.create_rectangle(10, 15, 75, 100,
fill="purple", outline="yellow", width=5)
myCanvas.create_oval(10, 15, 75, 100, fill="purple, outline="red",
width=9)
This creates an oval. How? Well, think of the rectangle we
created earlier--now think
of the biggest oval that fits in this rectangle--the top of the oval is
at the middle of the
top side of the rectangle, the leftmost point of the oval is at the
middle of the left side of
the rectangle, etc. No rectangle is actually drawn. NOTE
that the points (10,15) and
(75,100) are actually outside
the oval!
myCanvas_create_line(10, 15. 75, 100) tells Tk to
create a line from (10,15) to (75,100)
Options here include width, outline, and arrow--arrow="first" would put
an arrowhead
at (10,15). arrow="last" puts an arrowhead at (75,100),
arrow="both" puts arrowheads
on both ends of the line.