[英]Turtle Graphics - Retrieve Color of Overlapping Shapes
我有一个红色的星星,部分刻在包含橙色圆圈的紫色正方形内。 我正在提取用户单击的点的颜色。 当我单击正方形内的圆圈时,返回的颜色是紫色,而不是橙色。 当我单击正方形内的红色星形部分时,程序也会返回紫色。 如何解决此问题? 谢谢。
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()
我建议针对此问题的另一种方法。 建议您不要完全使用tkinter的基础来查找非活动对象的颜色(从乌龟的角度来看),而是建议您完全在乌龟中工作并使绘制的对象处于活动状态。 我们可以通过使每个绘图成为乌龟光标来实现此目的,以便我们单击乌龟并询问其颜色,这是一个更简单的问题:
import turtle
def rectangle(height):
length = height * 2
turtle.begin_poly()
for _ in range(2):
turtle.forward(length)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_poly()
return turtle.get_poly()
def star(vertices, steps, length):
angle = steps * 360.0 / vertices
turtle.begin_poly()
for _ in range(vertices):
turtle.forward(length)
turtle.right(angle)
turtle.end_poly()
return turtle.get_poly()
def circle(radius):
turtle.begin_poly()
turtle.circle(radius)
turtle.end_poly()
return turtle.get_poly()
def display_color(turtle):
print(turtle.fillcolor())
def main():
# Use the "default" turtle to draw the others
turtle.penup()
turtle.hideturtle()
turtle.setheading(90)
turtle.speed('fastest')
screen.register_shape('rectangle', rectangle(150))
screen.register_shape('star', star(5, 2, 50))
screen.register_shape('circle', circle(50))
rectangle_turtle = turtle.Turtle('rectangle')
rectangle_turtle.penup()
rectangle_turtle.color('purple')
rectangle_turtle.onclick(lambda x, y: display_color(rectangle_turtle))
star_turtle = turtle.Turtle('star')
star_turtle.penup()
star_turtle.setposition(150, 3)
star_turtle.color('red')
star_turtle.onclick(lambda x, y: display_color(star_turtle))
circle_turtle = turtle.Turtle('circle')
circle_turtle.penup()
circle_turtle.setposition(70, -107)
circle_turtle.color('orange')
circle_turtle.onclick(lambda x, y: display_color(circle_turtle))
screen = turtle.Screen()
main()
screen.mainloop()
现在,您应该能够单击任何填充的彩色区域,并且您将在控制台窗口中看到打印的颜色的名称。 (在单击窗口本身以使其激活之后。)
我看到两个问题
首先:请看上一个问题的代码-您必须获取最后一个元素ids[-1]
,而不是第一个ids[0]
才能获取最高元素。
第二:您将乌龟移到被点击的位置,所以现在乌龟是最高的-因此您可以在变色后移动鼠标,然后仍然可以使用
index = ids[-1]
否则您必须从结尾ids[-2]
获得第二个,但随后必须检查ids
是否至少包含两个元素
if len(ids) > 1: # if list has more than only turtle
index = ids[-2] # get second from the end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.