[Tutor] Drawing simple graphics objects

Kristinn Didriksson kdidriksson at gmail.com
Thu Nov 9 02:25:51 CET 2006


Hello,
This is one of the questions in Python Programming by Zelle. Ch 5, #1b.
The problem calls for the program to create a new square at the mouse  
click instead of moving the square as in the previous problem.
I have tried various ways to tackle the problem with no success. Any  
help would be greatly be appreciated.

This is the code that works for the previous example and moved the  
square at the mouse click:
------
# Modify the example in the book to program to draw a square instead  
of a circle.
# Interactive graphics program to draw a square

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()
		dx = p.getX() - c.getX()
		dy = p.getY() - c.getY()
		shape.move(dx,dy)
	win.close()
main()

#This one worked without too much trouble. All I had to do was change  
the object to be
# drawn to a square!
--------------------------
This is the mess i got myself into with the next problem. Please note  
that I initially tried to move the shape.draw(win) object to the  
beginning of the loop and I got an error that said:
Traceback (most recent call last):
   File "/Users/krissd/Desktop/python/code_zelle/chapter5/1a.py",  
line 21, in <module>
     main()
   File "/Users/krissd/Desktop/python/code_zelle/chapter5/1a.py",  
line 14, in main
     shape.draw(win)
   File "/Users/krissd/Desktop/python/code_zelle/chapter5/ 
graphics.py", line 280, in draw
     if self.canvas: raise GraphicsError, OBJ_ALREADY_DRAWN
graphics.GraphicsError: ('O', 'b', 'j', 'e', 'c', 't', ' ', 'c', 'u',  
'r', 'r', 'e', 'n', 't', 'l', 'y', ' ', 'd', 'r', 'a', 'w', 'n')
230-152:~/Desktop/python/code_zelle/chapter5 krissd$

Second program:
This one draws squares, but not of the same dimensions as the first  
and not with the center at the mouse click.
---------------------
# Modify the example in the book to program to draw a square instead  
of a circle.
# Additionally, have a new square drawn each time the mouse is clicked.

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
		p = win.getMouse()
		c = shape.getCenter()
		dx = p.getX() # - c.getX()
		dy = p.getY() # - c.getY()
		shape = Rectangle(Point(dx,dx),Point(dy,dy))
		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!!!!
----------------

I have attached the graphics package that is used in the program for  
reference.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: graphics.py
Type: text/x-python-script
Size: 21426 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20061108/51937d28/attachment-0001.bin 
-------------- next part --------------

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?

Regards,
kristinn


More information about the Tutor mailing list