tkinter, canvas, get color of image?

skanemupp at yahoo.se skanemupp at yahoo.se
Fri Apr 18 12:10:31 EDT 2008


On 16 Apr, 00:46, Bryan Oakley <oak... at bardo.clearlight.com> wrote:
> skanem... at yahoo.se wrote:
> > On 13 Apr, 19:19, Bryan Oakley <oak... at bardo.clearlight.com> wrote:
> >> skanem... at yahoo.se wrote:
> >>> mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif')
> >>> w.create_image(10, 10, image = mapq, anchor = NW)
> >>> after doing this is there any possibility of getting the
> >>> characteristics of the GIF-picture(or bitmap if i use that)?
> >>> it would be very helpfull if i for example could do something like
> >>> canvas.getcolor(image, mouseclick.x,mouseclick.y) if u get the point.
> >>> get the color of the image where i clicked.
> >> The image isn't "painted" on the canvas, so to answer your specific
> >> question, no, you can't get the color of a pixel on the canvas in the
> >> way that you ask.
>
> >> However, when you click on the canvas you can get the item that was
> >> clicked on and the x/y of the click. From that you can figure out which
> >> pixel of the image is under the cursor. And from that you can query the
> >> image for the color of a specific pixel.
>
> > how do i get the item?
> >http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_image-...
>
> > with any of those methods?
>
> > when i click the mouse i can get event.object u mean?
>
> You can use find_closest to find the object closest to the x,y of the
> event. You can also do find_withtag(tk.CURRENT) which returns the item
> under the mouse pointer.


Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "C:\Users\saftarn\Desktop\pythonkod\mapexperiments
\mapgetobject.py", line 17, in callback
    undermouse=find_closest(master.CURRENT)
NameError: global name 'find_closest' is not defined


from Tkinter import *

master = Tk()

w = Canvas(master, width=400, height=625)
w.pack(expand = YES, fill = BOTH)

mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\images
\something.gif')
w.create_image(30, 30, image = mapq, anchor = NW)

def key(event):
    print "pressed", repr(event.char)

def callback(event):
    clobj=event.widget
##    undermouse=find_withtag(master.CURRENT)
    undermouse=find_closest(master.CURRENT)
    print repr(undermouse)

w.bind("<Key>", key)
w.bind("<Button-1>", callback)
w.pack()

mainloop()



More information about the Python-list mailing list