""" clicks_v2.py version 2 : additional debugging after class * the MyRect class __init__ works. * the MyRect class check_click now works - added is_inside - ... and some print() debugging statements - ... and a screenshot 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): # Create and draw the Rectangle in the window rect = Rectangle(Point(row,column), Point(row+1, column+1)) rect.setFill('red') rect.setWidth(2) rect.draw(window) # remember the input parameters, and the Zelle Rectangle self.row = row self.column = column self.window = window self.rect = rect def is_inside(self, point): """ return True if point is inside this MyRect """ # # -----P2 the Rectangle self.rect has two corners points # | | which are P1 and P2. See Zelle's graphics API. # P1.---- # x = point.getX() y = point.getY() corner1 = self.rect.getP1() corner2 = self.rect.getP2() left = corner1.getX() right = corner2.getX() top = corner2.getY() # oops : I had these two reversed. bottom = corner1.getY() # debugging # ... to debug, uncomment the following print() statements, # and also change to grid=2 in main() so that we don't # get overwhelmed with printed output. #print("-- is_inside --") #print(" x, y = ", x, y) #print(" left, right, top, bottom = ", left, right, top, bottom) # Is this point at (x,y) within the rectangle (left,right,top,bottom) ? return left <= x and x <= right and bottom <= y and y <= top def check_click(self, point): """ change color if that point is in this rectangle """ print(f"check_click for rect at {self.row}, {self.column}") if self.is_inside(point): print(f" ... {point} is inside so turning it blue") 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, 3) 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()