[Tutor] Tk canvas question
Kent Johnson
kent37 at tds.net
Tue Aug 23 13:18:56 CEST 2005
Ìúʯ wrote:
> I am writing a resource manager like program !
> the files of directory is display on canvas and taged.
> the tag and file name is keep in a dictionary!
> The tag is a increaseing number from 1,so I build the
> dictionary like (1:file1,2:file2,......).
> While geting into another directory,I try to remove all
> object and tags on the canvas so that the tags would be
> count form 1 again.But I found out the tag number is
> increasing, not recount form 1. My dictionary can be
> organize that simple.
This sounds like a bug in your program; you should be able to start over with the tags. Can you show us some code?
> I want to know is there a way to get the tag of canvas
> recount form 1 again? or even more simple ,get the filename
> I had wrote in canvas directly?
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')
Maybe you could just remember the handles to the text items instead of using tags?
Kent
More information about the Tutor
mailing list