I have a red star partially inscribed within a purple square that contains an orange circle. I am extracting the color of the point that the user clicks on. When I click on the circle inside the square, the color that is returned is purple, not orange. The program also returns purple when I click on the part of the red star that is inside the square. How can I rectify this issue? Thank you.
import turtle
def border(height,color):
height = float(height)
length = height *(1.9)
length = round(length,2)
# Draws a rectangle.
turtle.begin_fill()
turtle.color(color)
turtle.down()
turtle.forward(length)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
def big_shape(vertices, steps, length):
turtle.color("red")
turtle.begin_fill()
for i in range(vertices):
turtle.forward(length)
turtle.right(steps*360.0/vertices)
turtle.end_fill()
def textbox_click(rawx,rawy):
turtle.up()
turtle.setposition(rawx,rawy)
turtle.down()
rawy = -rawy
canvas = turtle.getcanvas()
canvas.pack(fill="both", expand=True)
ids = canvas.find_overlapping(rawx, rawy, rawx, rawy)
if ids: # if list is not empty
index = ids[0]
color = canvas.itemcget(index, "fill")
if color != '':
print(color.lower())
def getcoordinates():
turtle.onscreenclick(turtle.goto)
turtle.onscreenclick(modifyglobalvariables) # Here's the change!
turtle.onscreenclick(textbox_click)
def modifyglobalvariables(rawx,rawy):
global xclick
global yclick
xclick = int(rawx//1)
yclick = int(rawy//1)
print(xclick)
print(yclick)
def main():
border(150,"purple")
turtle.begin_fill()
turtle.down()
turtle.color("purple")
turtle.up()
# Creates the big shape
x1=150
y1=3
turtle.setposition(x1,y1)
big_shape(5,2,50)
turtle.begin_fill()
turtle.down()
turtle.up()
# Circle
x1=70
y1=-107
turtle.setposition(x1,y1)
turtle.begin_fill()
turtle.circle(50)
turtle.color("orange")
turtle.end_fill()
getcoordinates()
turtle.done()
main()