【发布时间】:2017-11-27 00:54:12
【问题描述】:
我有一个红色的星星,部分刻在一个紫色的正方形中,里面有一个橙色的圆圈。我正在提取用户点击的点的颜色。当我单击正方形内的圆圈时,返回的颜色是紫色,而不是橙色。当我单击正方形内的红色星形部分时,程序也会返回紫色。我该如何纠正这个问题?谢谢你。
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()
【问题讨论】:
-
ids保留所有重叠的元素。也许紫色方块是最重要的元素。您可以查看ids中的其他值