469,311 Members | 2,406 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,311 developers. It's quick & easy.

Tkinter, getting canvas-object, how?

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\mapexperim ents
\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()
Jun 27 '08 #1
3 7101
skanem...@yahoo.se napisa³(a):
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\mapexperim ents
\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()
from Tkinter import *

master = Tk()

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

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))

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

mainloop()
Jun 27 '08 #2
On 19 Apr, 10:15, Rafał Wysocki <rafal.wyso...@gmail.comwrote:
skanem...@yahoo.se napisa³(a):
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\mapexperim ents
\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()

from Tkinter import *

master = Tk()

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

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))

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

mainloop()
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?
Jun 27 '08 #3
On 19 Apr, 21:43, globalrev <skanem...@yahoo.sewrote:
On 19 Apr, 10:15, Rafał Wysocki <rafal.wyso...@gmail.comwrote:
skanem...@yahoo.se napisa³(a):
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\mapexperim ents
\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()
from Tkinter import *
master = Tk()
w = Canvas(master, width=400, height=625)
w.pack(expand = YES, fill = BOTH)
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))
w.bind("<Key>", key)
w.bind("<Button-1>", callback)
w.pack()
mainloop()

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?
nevermind i get it now
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mksql | last post: by
1 post views Thread by Elaine Jackson | last post: by
1 post views Thread by syed_saqib_ali | last post: by
reply views Thread by syed_saqib_ali | last post: by
3 posts views Thread by Tuvas | last post: by
5 posts views Thread by Dean Allen Provins | last post: by
7 posts views Thread by Peter Pearson | last post: by
3 posts views Thread by joshdw4 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.