You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python Zelle graphics获取画布指定坐标的颜色?

Getting Pixel Color from Zelle Graphics Window

Hey there! I get it—you're working with Zelle's Graphics module to draw shapes, and you're stuck trying to grab the color of a specific coordinate because getPixel() only works for image files, not the shapes you draw directly in the window. Let's break down two solutions for you, depending on what you're actually trying to achieve.

Option 1: Grab the Exact Pixel Color (Using Tkinter Under the Hood)

Zelle's GraphWin is built on top of Tkinter, so we can use Tkinter's native methods to access pixel colors. The catch is we need to adjust for the coordinate system difference: Zelle uses a bottom-left origin, while Tkinter uses top-left.

Here's a helper function to get the RGB color of any coordinate in your GraphWin:

def get_pixel_color(win, x, y):
    # Convert Zelle coordinates to Tkinter's top-left origin
    tk_x = x
    tk_y = win.getHeight() - y  # Flip the y-axis to match Tkinter's system
    # Access the underlying Tkinter canvas from the Zelle window
    canvas = win.canvas
    # Get RGB values (Tkinter returns values in 0-65535 range; we convert to 0-255)
    rgb_values = canvas.winfo_rgb(canvas.winfo_containing(tk_x, tk_y))
    return tuple(val // 256 for val in rgb_values)

Now update your main code to use this function instead of getPixel():

from math import *
from time import *
from graphics import *

def get_pixel_color(win, x, y):
    tk_x = x
    tk_y = win.getHeight() - y
    canvas = win.canvas
    rgb_values = canvas.winfo_rgb(canvas.winfo_containing(tk_x, tk_y))
    return tuple(val // 256 for val in rgb_values)

def main():
    paper = GraphWin('shjaji20', 300, 300)
    paper.setBackground('white')
    road0 = Circle(Point(150, 150), 100)
    road1 = Line(Point(150, 50), Point(150, 0))
    road2 = Line(Point(50, 150), Point(0, 150))
    road3 = Line(Point(250, 150), Point(300, 150))
    road4 = Line(Point(150, 250), Point(150, 300))
    road0.draw(paper)
    road1.draw(paper)
    road2.draw(paper)
    road3.draw(paper)
    road4.draw(paper)
    car = Circle(Point(0, 150), 5)
    car.setFill('white')
    car.draw(paper)
    for i in range(1000):
        car.move(1, 0)
        time.sleep(.05)
        # Now this will print the RGB color of (150, 0) in Zelle's coordinate system
        print(get_pixel_color(paper, 150, 0))
main()

Option 2: Check if a Point is Inside a Shape (Better for Collision Detection)

If your goal is to detect when the car hits a road (like collision detection), you don't need to mess with pixel colors at all. Zelle's shape objects have a built-in contains() method that checks if a point is inside the shape using geometric calculations:

# Example: Check if the car's center is on the circular road (road0)
if road0.contains(car.getCenter()):
    print("Car is on the circular road!")

This is more reliable than pixel color checks, since it avoids issues like line thickness or fill color variations that might affect pixel values.

Why getPixel() Didn't Work

Zelle's getPixel() method is designed specifically for loading image files (with the Image class) and reading their pixel data—it doesn't interact with the shapes you draw dynamically in the GraphWin window. That's why switching to Tkinter's underlying methods or using the shape's contains() method is necessary.

内容的提问来源于stack exchange,提问作者Seungjun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:28:43