Python 如何清除 Tkinter 画布?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15839491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear Tkinter Canvas?
提问by Taylor Hill
When I draw a shape using:
当我使用以下方法绘制形状时:
canvas.create_rectangle(10, 10, 50, 50, color="green")
Does Tkinter keep track of the fact that it was created?
Tkinter 是否会跟踪它被创建的事实?
In a simple game I'm making, my code has one Framecreate a bunch of rectangles, and then draw a big black rectangle to clear the screen, and then draw another set of updated rectangles, and so on.
在我制作的一个简单游戏中,我的代码Frame创建了一堆矩形,然后绘制一个大的黑色矩形以清除屏幕,然后绘制另一组更新的矩形,依此类推。
Am I creating thousands of rectangle objects in memory?
我是否在内存中创建了数千个矩形对象?
I know you can assign the code above to a variable, but if I don't do that and just draw directly to the canvas, does it stay in memory, or does it just draw the pixels, like in the HTML5 canvas?
我知道你可以将上面的代码分配给一个变量,但如果我不这样做而直接在画布上绘制,它是留在内存中,还是只绘制像素,就像在 HTML5 画布中一样?
采纳答案by Bryan Oakley
Every canvas item is an object that Tkinter keeps track of. If you are clearing the screen by just drawing a black rectangle, then you effectively have created a memory leak -- eventually your program will crash due to the millions of items that have been drawn.
每个画布项目都是 Tkinter 跟踪的对象。如果您只是通过绘制一个黑色矩形来清除屏幕,那么您实际上已经造成了内存泄漏——最终您的程序将由于绘制了数百万个项目而崩溃。
To clear a canvas, use the deletemethod. Give it the special parameter "all"to delete all items on the canvas (the string "all"" is a special tag that represents all items on the canvas):
要清除画布,请使用delete方法。给它一个特殊的参数"all"来删除画布上的所有项目(字符串"all"“是一个特殊的标签,代表画布上的所有项目):
canvas.delete("all")
If you want to delete only certain items on the canvas (such as foreground objects, while leaving the background objects on the display) you can assign tags to each item. Then, instead of "all", you could supply the name of a tag.
如果您只想删除画布上的某些项目(例如前景对象,而将背景对象保留在显示器上),您可以为每个项目分配标签。然后,"all"您可以提供标签的名称而不是。
If you're creating a game, you probably don't need to delete and recreate items. For example, if you have an object that is moving across the screen, you can use the moveor coordsmethod to move the item.
如果您正在创建游戏,您可能不需要删除和重新创建项目。例如,如果您有一个在屏幕上移动的对象,您可以使用move或coords方法来移动该项目。
回答by Steven Rumbalski
Items drawn to the canvas are persistent. create_rectanglereturns an item id that you need to keep track of. If you don't remove old items your program will eventually slow down.
绘制到画布上的项目是持久的。 create_rectangle返回您需要跟踪的项目 ID。如果您不删除旧项目,您的程序最终会变慢。
From Fredrik Lundh's An Introduction to Tkinter:
来自 Fredrik Lundh 的Tkinter 简介:
Note that items added to the canvas are kept until you remove them. If you want to change the drawing, you can either use methods like
coords,itemconfig, andmoveto modify the items, or usedeleteto remove them.
请注意,添加到画布的项目会一直保留,直到您将其删除。如果要更改绘图,可以使用
coords、itemconfig、 和等方法move修改项目,或者使用delete删除它们。
回答by DaveTheScientist
Yes, I believe you are creating thousands of objects. If you're looking for an easy way to delete a bunch of them at once, use canvas tags described here. This lets you perform the same operation (such as deletion) on a large number of objects.
是的,我相信您正在创建数以千计的对象。如果您正在寻找一种简单的方法来一次性删除一堆,请使用此处描述的画布标签。这使您可以对大量对象执行相同的操作(例如删除)。

