""" clicks_v1.py version_1 : what I did in class. * the MyRect class __init__ works. * the MyRect class check_click does nothing - doesn't work. - - - - - - - - - - Here is a program that creates a grid of rectangles. (1) Put the rectangle creation into a MyRect class, so that the loop in make_rects becomes just for row in range(grid_size): for column in range(grid_size): rects.append(MyRect(row, column, window)) Hint: you'll need to create a store a Rectangle object within the MyRect. (2) Create a MyClickable class, a variation of the MyRect class which has a .check_click(point) method, which (a) checks to see if point is within the boundary of the rectangle, and (b) If it is, changes the color of the rectangle. Then at the end of the main() function, change it to while True: click_point = window.getMouse() for rect in rects: rect.check_click(click_point) ... and see what happens """ from graphics import * class MyRect: def __init__(self, row, column, window): self.row = row self.column = column self.window = window rect = Rectangle(Point(row,column), Point(row+1, column+1)) rect.setFill('red') rect.setWidth(2) rect.draw(window) self.rect = rect # remember the Rectangle def check_click(self, point): """ change color if that point is in this rectangle """ # # -----P2 # | | # P1.---- x = point.getX() y = point.getY() corner1 = self.rect.getP1() corner2 = self.rect.getP2() left = corner1.getX() right = corner2.getX() top = corner1.getY() bottom = corner2.getY() if left <= x <= right and bottom <= y <= top: self.rect.setFill('blue') def display_message(message, window): """ Display the given message in the window. """ text = Text(Point(4, -.5), message) text.setSize(24) text.setFace('arial') text.setStyle('bold') text.draw(window) def make_window(pixel_size, grid_size): """ Return a new graphics window """ window = GraphWin("clicks", pixel_size, pixel_size) window.setCoords(-1, -1, grid_size + 1, grid_size + 1) return window def make_rects(grid_size, window): """ Return a list of Rectangles """ rects = [] for row in range(grid_size): for column in range(grid_size): rect = MyRect(row, column, window) rects.append(rect) return rects def main(): (pixels, grid) = (600, 10) window = make_window(pixels, grid) rects = make_rects(grid, window) display_message("Click somewhere to change a color.", window) while True: point = window.getMouse() for rect in rects: rect.check_click(point) if __name__ == '__main__': main()