海龟图形 - 从屏幕点击中提取颜色 [英] Turtle Graphics - Extract Color from onscreenclick

查看:31
本文介绍了海龟图形 - 从屏幕点击中提取颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用 Turtle Graphics 来处理更大的程序.我可以使用turtle.onscreenclick返回用户点击的点

I am using Turtle Graphics in Python for a larger program. I am able to return the point that the user clicks on using turtle.onscreenclick

但是,我想提取用户单击的点的 RGB 颜色.这甚至可以在海龟图形中完成,如何完成?谢谢!

However, I would like to extract the RGB color of the point that the user clicks on. Can this even be done in turtle graphics and how can this be accomplished? Thank you!

import turtle

# Global variables specifying the point clicked

xclick = 0
yclick = 0

# Draw a rectangle that is red

height = float(50)
length = height *(1.9)
length = round(length,2)
turtle.begin_fill()
turtle.color("red")
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()

# Gets the click 

def getcoordinates():
    turtle.onscreenclick(turtle.goto)
    turtle.onscreenclick(modifyglobalvariables) 

# Modifies the global variables    

def modifyglobalvariables(rawx,rawy):
    global xclick
    global yclick
    xclick = int(rawx//1)
    yclick = int(rawy//1)
    print(xclick)
    print(yclick)

getcoordinates()
turtle.done()

推荐答案

turtle 没有获取像素颜色的功能.它使用 tkinter(和小部件 tkinter.Canvas - turtle.getcanvas())来显示所有内容,但它没有获取像素的功能颜色也是.

turtle doesn't have function to get pixel color. It uses tkinter (and widget tkinter.Canvas - turtle.getcanvas()) to display everything but it doesn't have function to get pixel color too.

Canvas 将所有内容保留为对象和 获取 tkinter 画布的像素颜色"的第二个答案 展示了如何获取 (x,y) 位置的对象的颜色.也许它对你有用.

Canvas keeps all as objects and second answer for "Get pixel colors of tkinter canvas" shows how to get color of object in position (x,y). Maybe it will work for you.

我做了工作示例

canvas 使用不同的坐标 - 它需要改变 y = -y

canvas uses different coordinates - it needed to change y = -y

import turtle

# --- functions --- (lower_case_names)

def get_pixel_color(x, y):

    # canvas use different coordinates
    y = -y

    canvas = turtle.getcanvas()
    ids = canvas.find_overlapping(x, y, x, y)

    if ids: # if list is not empty
        index = ids[-1]
        color = canvas.itemcget(index, "fill")
        if color != '':
            return color.lower()

    return "white" # default color 

def modify_global_variables(rawx,rawy):
    global xclick
    global yclick

    xclick = int(rawx)
    yclick = int(rawy)

    print(get_pixel_color(xclick, yclick))


def draw_rect(x1, y1, width, height, color):
    y1 = -y1
    canvas = turtle.getcanvas()
    canvas.create_rectangle((x1, y1, x1+width, y1+height), fill=color, width=0)

# --- main ---

# Global variables specifying the point clicked

xclick = 0
yclick = 0

# Draw a rectangle that is red

height = 50.0 # now it is float
length = height * 1.9
length = round(length, 2)

turtle.down()
turtle.color("RED")

turtle.begin_fill()

for _ in range(2):
    turtle.forward(length)
    turtle.right(90) 
    turtle.forward(height)
    turtle.right(90)

turtle.end_fill()

# Use tkinter.Canvas to draw rectangle

draw_rect(100, 100, length, height, 'green')

# Gets the click & Modifies the global variables    

turtle.onscreenclick(modify_global_variables) 

turtle.done()

这篇关于海龟图形 - 从屏幕点击中提取颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆