First off, you need to understand what window.getMouse() in rect1
does. Python's in
operator works by turning a in b
into the method call b.__contains__(a)
. Unfortunately, the Rectangle
class doesn't have a __contains__
method. This your immediate problem.
So, you need to use a different test. I suggest using Python's chained comparison operators to do the bounds checking yourself (there doesn't seem to be any library support for this in the graphics
module):
mouse = window.getMouse()
if rect1.p1.x < mouse.x < rect1.p2.x and rect1.p1.y < mouse.y < rect1.p2.y
rect1.setFill("black")
Note that I've changed the while
loop for an if
statement. If you want to repeatedly test mouse clicks until one hits the rectangle, you probably want to loop on while True
and then break
if the tested condition is true:
while True:
mouse = window.getMouse()
if rect1.p1.x < mouse.x < rect1.p2.x and rect1.p1.y < mouse.y < rect1.p2.y
rect1.setFill("black")
break