[Tkinter-discuss] getcolor on Canvas (but without Image)

Guilherme Polo ggpolo at gmail.com
Sun Nov 16 17:20:10 CET 2008


On Sun, Nov 16, 2008 at 11:55 AM, Gabor Kalman <Kalman_G at msn.com> wrote:
> I have a simple Rectangle drawn on Canvas. For example:
>
> from Tkinter import *
> #
> class Test(Frame):
>     #-------------------------------------------------------------
>     def __init__(self, master=None):
>         Frame.__init__(self, master)
>         Pack.config(self)
>         self.createWidgets()
>     #------------------------------------------------------------
>     def createWidgets(self):
>         self.draw = Canvas(self, width=600, height=400)
>         self.draw.create_rectangle(100, 100, 200, 150, fill="blue")
>         self.draw.pack(side=LEFT)
> #
> test = Test()
> test.mainloop()
>
> Question:
> =======
>
> How can I retrieve the color on the Canvas AFTER the Rectangle is drawn,
> say, at the coordinate  (110,110)   ???
>

You will need to think a bit different to solve this. Each thing in
your Canvas has an id, and you can query any id to discover things
about it, like its fill color. So your problem is how to find the id
of the item that is positioned in the point (110, 110). For that you
use the method find_overlapping which is part of Canvas.

rect_id = self.draw.find_overlapping(110, 110, 110, 110)[0]
print self.draw.itemcget(rect_id, 'fill')

find_overlapping returns a tuple of all items it found in the
specified rectangle (x1, y1, x2, y2) so I'm using just the first one
(in your example there will be just one). There are variations of the
above sample for different questions, so if it doesn't solve your
question entirely reconsider detailing it more.

> P.S.
>
> Of course if the Rectangle would be an Image (!!!)  I could do it with:
>
> from PIL import Image
> im = Image.open"Rectangle.jpg")
> x=110
> y=110
> pix = im.load()
> print pix[x, y]
>
> But that's NOT what I want.
>
>
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>



-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list