[Tutor] Tkinter, how to retrieve information about an object on canvas

Alan Gauld alan.gauld at btinternet.com
Thu Nov 15 09:48:56 CET 2012


On 15/11/12 02:32, Matheus Soares da Silva wrote:
>
> Hello, I would like to be able to get information from a Tkinter canvas
> object. (color, width, tags, root points, etc),
>
>   I wrote the following function that, with a canvas bind, returns me
> the widget that has been clicked on, the widget is returned as a tuple
> by the find_overlapping method.
>
> # detecting click
> def Hasclicked(e):
>      global obj
>      global lastClick
>      lastClick = [e.x, e.y]
>      obj = e.widget.find_overlapping(e.x, e.y, e.x, e.y)
>
>    So, there's any method I can use on 'obj' to get the attributes?

Do you want to get the attributes of the object within the canvas or of 
the canvas itself?
I'm not quite clear from your description?

In either case you can access an attribute of an object using standard 
Tkinter attribute access mechanism: obj['attributeName']

 >>> import Tkinter as tk
 >>> top = Tk()
 >>> top = tk.Tk()
 >>> l = tk.Label(top,text='hello')
 >>> l.pack()
 >>> l['text']
'hello'
 >>>

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list