我正在尝试创建一个函数,该函数将返回 Turtle 画布上指定坐标处的填充颜色。我使用海龟库中的 find_overlapping 方法来查找画布上的对象,但此方法并不总是返回所需的结果。
这是我当前的方法:
from turtle import *
def get_pixel_color(x, y):
y = -y
canvas = getcanvas()
ids = canvas.find_overlapping(x, y, x, y)
if ids:
index = ids[-1]
color = canvas.itemcget(index, "fill")
if color:
return color
return "white"
但是,即使画布上的指定坐标处有填充,此方法有时也会返回“白色”。如何正确确定 Turtle 画布上特定坐标处的填充颜色?
我的代码示例
from turtle import *
def get_pixel_color(x, y):
y = -y
canvas = getcanvas()
ids = canvas.find_overlapping(x, y, x, y)
print(ids)
if ids:
index = ids[-1]
color = canvas.itemcget(index, "fill")
print(canvas.itemcget(index, "fill"))
if color:
return color
return "white"
def frange(start, stop, step):
while start <= stop:
yield start
start += step
SIZE = 40
tracer(0)
screensize(14000, 14000)
color('red')
position = pos()
left(90)
color('yellow')
begin_fill()
for i in range(5):
fd(9 * SIZE)
rt(120)
end_fill()
color('red')
up()
setpos(position)
rt(120)
down()
for i in range(5):
fd(9 * SIZE)
rt(120)
up()
update()
c = 0
for x in range(-5, 10):
for y in range(-5, 13):
real_x, real_y = x * SIZE, y * SIZE
print(get_pixel_color(real_x, real_y), pencolor())
if get_pixel_color(real_x, real_y) == 'yellow':
setpos(real_x, real_y)
dot(5, 'blue')
continue
done()
如有任何帮助,我们将不胜感激!
我建议使用截图功能来实现此目的:
from turtle import *
from time import sleep
import mss
import mss.tools
# Get the position and size of the turtle canvas window
shape('turtle')
screen=Screen()
screen.setup(width=350, height=300)
screen.setworldcoordinates(-175, -150, 175, 150)
def get_pixel_color(x, y):
window = screen.getcanvas().winfo_toplevel()
window_pos = (window.winfo_x(), window.winfo_y())
canvas_width = window.winfo_width()
canvas_height = window.winfo_height()
print(f"{window_pos=} ( {canvas_width=}, {canvas_height=} )" )
screen_x = window_pos[0] + canvas_width//2 + x
screen_y = window_pos[1] + canvas_height//2 - y
with mss.mss() as sct:
monitor = { "top": screen_y, "left": screen_x, "width": 1, "height": 1 }
img = sct.grab(monitor)
color = img.pixel(0, 0)
return '#{:02x}{:02x}{:02x}'.format(color[0], color[1], color[2])
screensize( 300, 300)
SIZE = 150 ; pensize(7)
# tracer(0)
color('red') ; begin_fill();fd(SIZE);rt(120);fd(SIZE);rt(120);fd(SIZE);end_fill()
color('green') ; begin_fill();fd(SIZE);rt(120);fd(SIZE);rt(120);fd(SIZE);end_fill()
color('blue') ; begin_fill();fd(SIZE);rt(120);fd(SIZE);rt(120);fd(SIZE);end_fill()
rt(60)
color('darkred') ; begin_fill();fd(SIZE);rt(120);fd(SIZE);rt(120);fd(SIZE);end_fill()
color('darkgreen'); begin_fill();fd(SIZE);rt(120);fd(SIZE);rt(120);fd(SIZE);end_fill()
color('darkblue') ; begin_fill();fd(SIZE);rt(120);fd(SIZE);rt(120);fd(SIZE);end_fill()
update()
# Sleep for a short time to ensure the canvas is updated
sleep(0.5)
# Check colors at random positions
color('white'); pu()
colorPositions=[ None, (0, -55 ), (0,55), (-75,55), (-75,-55), (75,55), (75,-55)]
for colorNo in range( 1, 6 +1):
x , y = colorPositions[colorNo]
color = get_pixel_color(x, y)
sleep(0.33)
goto(x,y); stamp(); write(f" {colorNo}", font=("Arial",12))
print(f"Color {colorNo} at ({x:3d}, {y:3d}): {color}")
done()
window_pos=(790, 419) ( canvas_width=350, canvas_height=300 )
Color 1 at ( 0, -55): #8b0000
window_pos=(790, 419) ( canvas_width=350, canvas_height=300 )
Color 2 at ( 0, 55): #008000
window_pos=(790, 419) ( canvas_width=350, canvas_height=300 )
Color 3 at (-75, 55): #00008b
window_pos=(790, 419) ( canvas_width=350, canvas_height=300 )
Color 4 at (-75, -55): #0000ff
window_pos=(790, 419) ( canvas_width=350, canvas_height=300 )
Color 5 at ( 75, 55): #006400
window_pos=(790, 419) ( canvas_width=350, canvas_height=300 )
Color 6 at ( 75, -55): #f0f0f0