I have a window and I'm rendering an image onto it. Every frame the position changes, so the problem is that (obviously) it doesn't disappear from the last frame. I want to clear the window every frame. I can't find any clear methods for the window object (GraphWin) anywhere. And I can't use undraw() because then I'd have to do it for everything on the screen.
-
You have to do something for everything in the window. However because `GraphWin` is a subclass of a `Tkinter.canvas` widget it means it has `find_all()` and `delete()` methods since `GraphWin` doesn't override them (from looking at the source for the module). The former will return a list of all the object ID numbers on the canvas. You can then iterate through these numbers in the list and call the `delete()` method on each one. – martineau Aug 05 '17 at 04:48
1 Answers
First thing to consider is if you're simply moving graphic objects between frames, then call their move()
methods rather than erase and redraw them. If you must clear the screen, then I suggest:
Before dropping down to the Tkinter level, I'd consider using Zelle Graphics' own underpinnings. The reason is that Zelle Graphics keeps it's own parallel records of objects and if you delete them from the Tkinter level, you could get the two out of sync. Here's my suggestion:
def clear(win):
for item in win.items[:]:
item.undraw()
win.update()
However, undrawing items is slow, likely slower than your desired frame rate. So, you'll want to turn off auto flush:
win = GraphWin(..., autoflush=False)
and then call:
update()
whenever you have something to present to your user -- this will speed up the graphics since it won't show all the intermediate steps.
The above advice does not apply to things drawn with the win.plot()
method, however. Plotting is implemented at a lower level than other Zelle graphics so you do need to drop down to Tkinter to clear the plot. See How to undraw plot with Zelle graphics?
for an example.

- 40,441
- 5
- 32
- 81