[Edu-sig] graphics programming with Python
Daniel Yoo
dyoo@hkn.eecs.berkeley.edu
Wed, 9 May 2001 03:16:29 -0700 (PDT)
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
Send mail to mime@docserver.cac.washington.edu for more info.
--545289610-1210645831-989403251=:13714
Content-Type: TEXT/PLAIN; charset=US-ASCII
Here's a preliminary version of a simplified interface to a Tkinter
canvas. It's simple enough so that the user just needs to type:
###
from graphics import *
def drawBox(x1, y1, x2, y2):
"""Draws a box where the upper left is (x1, y1), and the
lower right is (x2, y2)."""
positionPen(x1, y1)
drawLineTo(x2, y1)
drawLineTo(x2, y2)
drawLineTo(x1, y2)
drawLineTo(x1, y1)
drawBox(-100, 100, 100, -100)
###
to get a box drawn. By default, the Canvas is 400x400, and the user can
send commands like:
drawLine
drawPoint
setRGBColor
without having to worry about creating a canvas. Also, it uses normal
rectangular coordinates, so (0, 0) is at the center, (200, 200) is the
upper right corner, and (-200, -200) is the bottom left.
Please give suggestions on this; I just typed this up yesterday, so it
surely has room for improvement and clarity.
I hope this is useful!
--545289610-1210645831-989403251=:13714
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="graphics.py"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0105090314110.13714@hkn.eecs.berkeley.edu>
Content-Description:
Content-Disposition: attachment; filename="graphics.py"
IiIiVGhpcyBpcyBhIHNtYWxsIGdyYXBoaWNzIG1vZHVsZSB0aGF0IG1pbWlj
cyBzb21lIHRoZSBmdW5jdGlvbmFsaXR5DQpvZiB3aGF0IHRoZSBDUzMgc3R1
ZGVudHMgdXNlIHRvIGRvIGZyYWN0YWwgZ3JhcGhpY3MuICBJdCdzIHNpbXBs
aWZpZWQNCmJlY2F1c2UgdGhlIGNvb3JkaW5hdGVzIGFyZSBjZW50ZXJlZCBh
dCAoMCwgMCkgaW4gdGhlIG1pZGRsZSBvZiB0aGUNCnNjcmVlbiBhbmQgaXQg
dXNlcyBmYW1pbGl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