简体   繁体   English

如何清除 Tkinter 画布?

[英]How to clear Tkinter Canvas?

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 Frame create 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 画布中一样?

Every canvas item is an object that Tkinter keeps track of.每个画布项目都是 Tkinter 跟踪的对象。 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.如果您只是通过绘制一个黑色矩形来清除屏幕,那么您实际上已经造成了内存泄漏——最终您的程序将由于绘制了数百万个项目而崩溃。

To clear a canvas, use the delete method.要清除画布,请使用delete方法。 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):给它一个特殊的参数"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 move or coords method to move the item.例如,如果您有一个在屏幕上移动的对象,您可以使用movecoords方法来移动该项目。

Items drawn to the canvas are persistent.绘制到画布上的项目是持久的。 create_rectangle returns an item id that you need to keep track of. create_rectangle返回您需要跟踪的项目 ID。 If you don't remove old items your program will eventually slow down.如果您不删除旧项目,您的程序最终会变慢。

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 , and move to modify the items, or use delete to remove them.如果要更改绘图,可以使用coordsitemconfigmove方法修改项目,或者使用delete删除它们。

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.这使您可以对大量对象执行相同的操作(例如删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM