如何在不重绘每个元素的情况下更新画布? [英] How can I update an canvas without redrawing every element?

查看:38
本文介绍了如何在不重绘每个元素的情况下更新画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个画布,它有某种由 tkinter 形状绘制的背景,在顶部我正在移动一个圆圈.

Let's say I have an canvas, it has some kind of background drawn out of tkinter shapes, and on top I'm moving a circle around.

是否可以只重画圆圈,而不是每次都重画背景?

Is it possible to redraw just the circle, and not redraw the background every time?

示例代码:

import  Tkinter

class gui(Tkinter.Tk):  

    def draw_background(self):
        self.canvas.create_oval(0,0, 500, 500, fill = 'black')

    def draw_circle(self,x, y):
        self.canvas.create_oval(x,y, x+10,y+10, fill = 'green')



    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.guiHeight = 800
        self.guiWidth = 800
        self.initialise()

    def animation(self):

        self.x = self.x +1%100
        self.y = self.y +1%100

        self.canvas.delete("all")
        self.draw_background()      
        self.draw_circle(self.x, self.y)

        self.after(100,self.animation)

    def initialise(self):
        self.title('traffic')

        self.canvas = Tkinter.Canvas(self, height = self.guiHeight, width = self.guiWidth)
        self.draw_background()      
        self.canvas.pack()

        self.x = 0
        self.y = 0
        self.after(100, self.animation)
        self.mainloop()


if __name__ == "__main__":
    app = gui(None) 

这段代码将实现我想要它做的事情.一个绿点向右下角移动,并显示在背景圆圈的顶部.

This code will achieve exactly what I want it to do. A green dot moves toward the bottom right corner, and displays it self on top of the background circle.

但是,每次都告诉它重绘背景图好像很浪费(想象一下,如果绘制背景涉及很多计算)是否可以在透明图层上显示,然后重绘图层?

However, it seems wasteful to tell it redraw the background image every time (Imagine if there was a lot of calculation involved in drawing the background) Is it possible to display at transparent layer on top of it, and just redraw the layer?

推荐答案

您可以使用 移动 方法.您还可以删除任何一项并重新绘制.这些都不需要重绘任何其他对象.

You can move any item on a canvas with the move method. You can also delete any one item and redraw it. Neither of those require redrawing any other objects.

当您创建一个项目时,会返回一个 ID,您可以将其提供给移动或删除方法.

When you create an item, an ID is returned which you can give to the move or delete methods.

self.circle_id = self.canvas.create_oval(x,y, x+10,y+10, fill = 'green')
...
# move the circle 10 pixels in the x and y directions
self.canvas.move(self.circle_id, 10,10)

你也可以给一个或多个元素一个标签(实际上是一个标签列表),然后在一个命令中移动或删除所有带有该标签的元素:

You can also give one or more elements a tag (actually, a list of tags), and then move or delete all of the elements with that tag in a single command as well:

self.canvas.create_oval(x, y, x+10, y+10, fill='green', tags=("circle",))
...
self.canvas.move("circle", 10, 10)

您还可以计算圆的所有新坐标,然后使用 坐标 方法:

You can also compute all new coordinates for the circle and then update it with the coords method:

# make the upper-left corner 0,0 and the lower right 100,100
self.canvas.coords("circle", 0,0,100,100)

这篇关于如何在不重绘每个元素的情况下更新画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆