Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tkinter, getting canvas-object, how?

27 views
Skip to first unread message

skan...@yahoo.se

unread,
Apr 18, 2008, 12:20:26 PM4/18/08
to
so i load a gif onto a canvas and when i click the canvs i want to get
the color of the pixel that is clicked.
so i need to ge the object im clicking.
i was told in another thread to use find_withtag or find_closest but
it is not working, maybe im using the
method on the wrong object.
how do i do this?
and how do i then get specifics about that object, ie the pixel-color?

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\Maps\provinces-of-
sweden.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()

Rafał Wysocki

unread,
Apr 19, 2008, 4:15:13 AM4/19/08
to
skanem...@yahoo.se napisał(a):

from Tkinter import *

master = Tk()

mapq = PhotoImage(file = 'img.gif')
_id = w.create_image(0, 0, image = mapq, anchor = NW)

objects = {} # map id to object
objects[_id] = mapq

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

def callback(event):
x, y = w.canvasx(event.x), w.canvasy(event.y) # Translates a
window x,y coordinates to a canvas coordinate
_id = w.find_closest(x,y)[0] # Returns tuple containing the object
id
obj = objects[_id] # Finds object with given id
print 'color: %s' % obj.get(int(x), int(y))

globalrev

unread,
Apr 19, 2008, 3:43:21 PM4/19/08
to
On 19 Apr, 10:15, Rafał Wysocki <rafal.wyso...@gmail.com> wrote:
> skanem...@yahoo.se napisa³(a):

ty very much. however i dont get the %s really. is % a symbol and then
replaced by obj.get-values?
anyway when clicked i get 3values, but there is intx and inty only.
where does the 3rd value come from and how do i refer to it?

globalrev

unread,
Apr 19, 2008, 4:40:27 PM4/19/08
to
On 19 Apr, 21:43, globalrev <skanem...@yahoo.se> wrote:

nevermind i get it now

0 new messages