Canvas-Widget .... Color at position x,y

Eric Brunel eric_brunel at despammed.com
Wed Jun 2 06:52:24 EDT 2004


Matthias wrote:
> Hello,
> 
> I have a Canvas-Widget and will use as a "array of pixel". At Positon
> x,y I print a rectangle with a special color. I give the rectangle no
> objectname. Then I will ask the "root-Canvas-Widget" for the color in
> position x,y like:
> 
> color=cw.cget('bg',x,y)
> 
> I need HELP :))

You can get the tags for the objects at position (x, y) via:
tags = cw.find_overlapping(x, y, x, y)
(or maybe cw.find_overlapping(x, y, x+1, y+1); I didn't test...)
If you're sure you've got only one object overlapping this position, you can 
then do:
color = cw.itemcget(tags[0], 'bg')

BTW, using a Canvas as an array of pixels does not seem like a good idea to me: 
canvases are intended to do vector drawing, not bitmap...

Are you aware that you can do what you want via images? Example:

--images.py---------------------
from Tkinter import *

root = Tk()
cnv = Canvas(root)
cnv.pack(side=TOP)
img = PhotoImage(width=100, height=100)
cnv.create_image(0, 0, anchor=NW, image=img)

def plot():
   for i in range(10, 90):
     img.put('#ff0000', to=(i, i))
     img.put('#00ff00', to=(i, 100 - i))

def read():
   for i in range(0, 100):
     print img.get(i, 40)

Button(root, text='Plot', command=plot).pack(side=LEFT)
Button(root, text='Read', command=read).pack(side=LEFT)

root.mainloop()
--------------------------------

This is much easier to do (except maybe for the strange format of the color 
returned by img.get) and images are intended for this purpose, so you're less 
likely to have any problem.

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list