Skip to content Skip to sidebar Skip to footer

Get Color Of Coordinate Of Figure Drawn With Python Zelle Graphics

In python, How can I get the color of a specific coordinate of any figure, that I've drawn with the Zelle graphics module? I'm using Python with the Zelle graphics module to proces

Solution 1:

This can be done, in a way. Zelle's graphics.py is built atop Python's tkinter library which can both identify which graphic object sits above a given point as well as interrogate that object's color. The key is knowing that a GraphWin instance is also a tkinter Canvas by inheritance:

from time import sleep
from graphics import *

paper = GraphWin(width=300, height=300)

road = Circle(Point(150, 150), 100)
road.setFill('pink')
road.draw(paper)

Line(Point(150, 50), Point(150, 0)).draw(paper)
Line(Point(50, 150), Point(0, 150)).draw(paper)
Line(Point(250, 150), Point(300, 150)).draw(paper)
Line(Point(150, 250), Point(150, 300)).draw(paper)

car = Circle(Point(0, 150), 5)
car.setFill('white')
car.draw(paper)

for _ inrange(300):
    car.move(1, 0)

    center = car.getCenter()
    overlapping = paper.find_overlapping(center.x, center.y, center.x, center.y)
    if overlapping:
        print(paper.itemcget(overlapping[0], "fill"))

    sleep(0.05)

As the small circle moves across the lines, "black" will be printed to the console. As it moves across the central circle, "pink" we be printed. The code is for Python3, if you're using Python2 you'll need to adjust.

Post a Comment for "Get Color Of Coordinate Of Figure Drawn With Python Zelle Graphics"