[Tutor] Tk canvas question

=?gb2312?B?zPrKrw==?= winglion1 at 163.com
Wed Aug 24 05:45:45 CEST 2005


>You can use the itemcget() method of the canvas to retrieve the text:
>
> >>> root = Tk()
> >>> canvas=Canvas(root)
> >>> t=canvas.create_text(10, 10, text='Hello', tag='1')
> >>> canvas.pack()
> >>> canvas.itemcget('1', 'text')
>'Hello'
>
>There does seem to be something strange with reusing the tags - if I create a new element with tag '1' I can't retrieve it by tag:
>
> >>> canvas.delete('1')
> >>> t=canvas.create_text(10, 10, text='Goodbye', tag='1')
> >>> canvas.itemcget('1', 'text')
>''
> >>> canvas.find_withtag('1')
>()
>
>even though if I ask what tags are on the item it says '1':
>
> >>> t
>2
> >>> canvas.itemcget(2, 'text')
>'Goodbye'
> >>> canvas.itemcget(2, 'tags')
>'1'
> >>> canvas.find_withtag('1')
>

  Thank's for your help,kent! the itemcget method is exactly what I want!
In the first email,I mix the object id and tag,the increasing 
number I talk about is the object id.In the old source,I coded like :
    tagnum=self.find_closest(event.x,event.y)
and then use this tagnum as a tag to find the file name in my dictionary.
I had hope that this id would be count form 1 again ,but it won't as
I had found out. A silly mistake I had make!
  now, as your help,I bind the tag and object id in my dictionary:
		icon=self.create_image(10, i*self.dist_y, image=self.fileIcon, anchor=NW,tag=str(i))
	    txt=self.create_text(30,i*self.dist_y,text=elem,anchor=NW,tag=str(i))
        self.bindings[i]=txt
and then use itemcget to get the filename.
    k=self.itemcget (CURRENT, "tag")
    l=int(k.split()[0])
    elem=self.itemcget(self.bindings[l],"text")
 I had try find_withtag(l) here and get only one number at the first directory
  (num,)
  if i change to other directory and del tags:
   self.dtag(str(i)) and tag the new items with the same tag
   () is return. 
This is strange!
no matter what, my problem had served thank's alot!

winglion
2005-08-24





More information about the Tutor mailing list