I needed to know whether it is possible to clear all objects within a given set of coordinates regardless of the name of the object. I don't have any code for this at the moment as I've been trying to brainstorm this for a while now but have come up with nothing. An example I can give is that there are squares and circles within the coordinates of (200,100) and (300,200) how would I delete everything within these coordinates?
Yes, this can be done. First, the GraphWin
object keeps track of items drawn on it but doesn't officially export this list. So you should keep track of what you draw onto the window.
The items you want (e.g. Rectangle
and Circle
) belong to the class _BBox
which has a getCenter()
method you can use to manipulate objects within your boundaries:
from random import randrange
from graphics import *
win = GraphWin("My Example", 400, 400)
boundary = Rectangle(Point(200, 100), Point(300, 200))
boundary.setOutline('blue')
boundary.draw(win)
graphics = []
for _ in range(150):
circle = Circle(Point(randrange(400), randrange(400)), 5)
circle.setOutline('green')
circle.draw(win)
graphics.append(circle)
x, y = randrange(400), randrange(400)
rectangle = Rectangle(Point(x, y), Point(x + 10, y + 10))
rectangle.setOutline('orange')
rectangle.draw(win)
graphics.append(rectangle)
for graphic in graphics:
center = graphic.getCenter()
if 200 < center.getX() < 300 and 100 < center.getY() < 200:
graphic.setFill('red')
graphic.undraw()
win.getMouse()
win.close()