# [Tutor] Drawing simple graphics objects

Luke Paireepinart rabidpoobear at gmail.com
Thu Nov 9 09:36:44 CET 2006

```<snip lots of stuff>
>
> from graphics import *
>
> def main():
>
>     win = GraphWin()
>     shape = Rectangle(Point(50,50), Point(20,20))
>     shape.setOutline("red")
>     shape.setFill("red")
>     shape.draw(win)
>     for i in range(10): gui
What's this mysterious 'gui' floating out here?
>         p = win.getMouse()
>         c = shape.getCenter()
>         dx = p.getX() # - c.getX()
>         dy = p.getY() # - c.getY()
>         shape = Rectangle(Point(dx,dx),Point(dy,dy))
Do you understand what's happening here, and what you're trying to do?

The standard way to define rectangles is topleft, and the width, and
height, but this graphics library doesn't seem to do that (it uses
absolute coordinates)
one way to standardize this would be to define an intermediate function
that will convert width, height values to absolute coordinates,
so your code would look like this:

# start code.
from graphics import *

def make_rect(topleft,size):
return
(Point(topleft[0],topleft[1]),Point(topleft[0]+size[0],topleft[1]+size[1]))
def main():

win = GraphWin()
shape = Rectangle(Point(50,50), Point(20,20))
shape.setOutline("red")
shape.setFill("red")
shape.draw(win)
for i in range(10):
p = win.getMouse()
c = shape.getCenter()
rect = make_rect((p.getX(),p.getY()),(30,30))
shape = Rectangle(rect[0],rect[1])
shape.setOutline("red")
shape.setFill("red")
shape.draw(win)
win.close()
main()
#end code.

Otherwise, you can just use the actual absolute values if you don't want
to use this function...
#start code
from graphics import *
def main():
win = GraphWin()
shape = Rectangle(Point(50,50), Point(20,20))
shape.setOutline("red")
shape.setFill("red")
shape.draw(win)
for i in range(10):
p = win.getMouse()
c = shape.getCenter()
shape = Rectangle(Point(p.getX()+30,p.getY()+30),
Point(p.getX(),p.getY()))
shape.setOutline("red")
shape.setFill("red")
shape.draw(win)
win.close()
main()
#end code.

Note that both of these codes put the topleft of the image at the mouse
cursor.
If you want them to be centered, try to figure out how to do it yourself,
and if you can't, of course, we're here to help :)

>         shape.setOutline("red")
>         shape.setFill("red")
>         shape.draw(win)
>     win.close()
> main()
>
> #This is turning out trickier than I thought!
> # I have gotten close.  It redraws the square, but not with the same
> dimensions
> # at a different location determined by the mouse click.
> # I am barking up the wrong tree with this effort!!!!
Look at the code I sent and see if you can see what the problem was.
(I'm really tired now and I can't tell you exactly what is wrong without
making an effort,
and I'm saving myself for the Computer Ethics essay I have yet to write :)

> ----------------
>
> I have attached the graphics package that is used in the program for
> reference.
> ------------------------------------------------------------------------
> <snip very large graphics.py file>
thanks for giving it as reference.  If you could've pastebinned it or
something it would've been nice cause it was quite a long file.
>
> ------------------------------------------------------------------------
>
>
> One additional question. Zelle creates this graphics library to use to
> demonstrate GUI programming. I have seen discussions about tkinter and
> wxpython. Should I be trying to use one of them instead for these
> problems? Or would that be jumping ahead of the game?
This graphics library is written in TKinter.
I would be using pygame instead for these problems.
The stuff I've seen so far, drawing rects, etc. has nothing to do with
TKInter and wxpython.
Those libraries are for making e-mail clients, web browsers, and stuff.
things with lots of listboxes, scroll bars,
and text.  Not for playing with images.  That's what pygame is for.
But I think you should use the graphics library if you can, since it's
what the book is written about,
unless you really feel that you can dive into a new library without the
book's help.

HTH,
-Luke
```