-->

Tkinter Canvas reset ID's

2019-08-05 12:14发布

问题:

I am coding a Tkinter GUI for a senior research project at school.

I need to, at some point in the code, reset the counter used to create id values for new additions. Does anyone know if this is possible beside replacing the entire canvas? Basically, after adding a bunch of lines and ovals, I need to delete them all and restart the counter at 1 for new ones.

Second, I have taken the path of replacing the entire canvas, and afterwards, nothing EVER shows up in the winfo_get() method. Why could this be? I know I'm trying to do something that TKinter isn't supposed to do, but it must be possible.

回答1:

It is not possible to reset the id numbers generated by a canvas.

Your solution of deleting and recreating the canvas is a reasonable alternative, though it may not be necessary. There are probably better ways to do whatever it is you think is solved by resetting canvas ids. For example, you could generate your own ids, and associate each id with each canvas item as a tag. You can then reset the ids any time you want.