473,692 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "C:\Users\safta rn\Desktop\pyth onkod\mapexperi ments
\mapgetobject.p y", 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\safta rn\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.wid get
## 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 7467
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\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "C:\Users\safta rn\Desktop\pyth onkod\mapexperi ments
\mapgetobject.p y", 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\safta rn\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.wid get
## 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\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "C:\Users\safta rn\Desktop\pyth onkod\mapexperi ments
\mapgetobject.p y", 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\safta rn\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.wid get
## 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...@yaho o.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\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "C:\Users\safta rn\Desktop\pyth onkod\mapexperi ments
\mapgetobject.p y", 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\safta rn\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.wid get
## 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5978
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles filling the remainder of the window. Mind you, this is a contrived test application to help me understand Tkinter and Python, not an actual application yet. I've trivially subclassed Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases...
2
4005
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the extra step is necessary. This causes the "graph" function to execute when the button is created: Button(root, text='OK', command=graph(canvas))) However, this waits until the button is pressed (the desired behavior): def doit(): graph(canvas)
1
10621
by: Elaine Jackson | last post by:
Newbie. Playing with the 'turtle' module and wondering if there's a way to save the graphics you make with it. The documentation itself has nothing to say about this, nor (as far as I can tell) does the FAQ list at python-dot-org. Any pointers would be very much appreciated. (In related news, I would also like to find out what's going on with "bounding boxes" in image files - not having them is keeping me from including images in latex...
1
2972
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
0
3581
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
3
4493
by: Tuvas | last post by:
I'm trying to display a picture on a Tkinter Canvas. It seems to work fine the first time that it is displayed. However, subsequent times running shows an error like this: TCLerror: Wrong # args: should be ".-1211472948 .-1211470996 addtag tag searchCommand ?arg arg ...? My code works like this: if (pic):
5
14968
by: Dean Allen Provins | last post by:
I need to determine the size of a canvas while the process is running. Does anyone know of a technique that will let me do that? Thanks, Dean
1
2899
by: apriebe47 | last post by:
Hello, I have created a simple canvas in Tkinter to display a number of PhotoImages, I then bind a key (in my case, <Up>) to start a loop that plays through a list of PhotoImages to make it an animation of sorts. What I noticed is, after holding down the key for a certain time, it would not loop through all 6 animations, instead it would just go through the first 2 and reset it self. I changed the key bind to Button-1 and did not...
7
4706
by: Peter Pearson | last post by:
Tkinter makes it very easy to drag jpeg images around on a canvas, but I would like to have a "target" change color when the cursor dragging an image passes over it. I seem to be blocked by the fact that the callbacks that might tell the target that the mouse has entered it (<Enter>, <Any-Enter>, even <Motion>) aren't called if the mouse's button is down. What am I missing? Have I failed to find the right Tkinter document? Is Tkinter...
3
2981
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
0
8603
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8800
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5818
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2237
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1957
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.