清除 Python 中 Window 的图形

Clearing Graphics from a Window in Python

所以我试图制作一个定时循环,其中不同的文本命令出现在屏幕上,用户必须按下与文本命令对应的键。问题是因为文本是居中的,它只是在自身上绘制,直到命令不可读。每次我想将文本绘制到 window 时,有没有一种方法可以清除 window?我在下面发布了我当前的代码

from Graphics import *
from Myro import timer  #used for the animation.
import random   #random starting positions and speeds

NUMBALLS = 10
WINSIZE = 500
done = 0
win = Window("GAME", WINSIZE, WINSIZE)
#win.mode = "manual"  #Trick to make the animation smoother....

space = Text((180, 250), "SPACEBAR")
space.fill = Color("blue")
space.fontSize = 30
space.xJustification = "bottom"
space.yJustification = "bottom"

up = Text((180, 250), "UP")
up.fill = Color("red")
up.fontSize = 30
up.xJustification = "bottom"
up.yJustification = "bottom"

down = Text((180, 250), "DOWN")
down.fill = Color("black")
down.fontSize = 30
down.xJustification = "bottom"
down.yJustification = "bottom"

left = Text((180, 250), "LEFT")
left.fill = Color("green")
left.fontSize = 30
left.xJustification = "bottom"
left.yJustification = "bottom"

right = Text((180, 250), "RIGHT")
right.fill = Color("orange")
right.fontSize = 30
right.xJustification = "bottom"
right.yJustification = "bottom"

shape = Rectangle((0, 500), (500, 0))
shape.fill = Color("white")


keys = (space,up,left,right,down)

def handleKeyPress(win, event):
    global done
    if ((this == 0) and (done == 0)):
        if (event.key == "space"):
            print("correct")
            done = 1
    if ((this == 1) and (done == 0)):
        if (event.key == "Up"):
            print("correct")
            done = 1
    #if ((this == 2) and (done == 0)):
     #   if (event.key == "Left"):
        #    print("correct")
         #   done = 1
    #if ((this == 3) and (done == 0)):
   #     if (event.key == "Right"):
         #   print("correct")
            done = 1
   # if ((this == 4) and (done == 0)):
     #   if (event.key == "Down"):
    #        print("correct")
      #      done = 1


for i in timer(5):
    shape.draw(win)
    this = random.randint(0,4)
    me = keys[this]
    me.draw(win)
    onKeyPress(handleKeyPress)
    print("done")

    wait(0.4)
    done = 0

编辑:显然,您使用的是 Calico 图形,它是基于 IronPython 而不是 Cython 构建的,并且无法访问 tkinter 绑定。底层实现是 Gtk,而不是 tkinter,如果您正在尝试扩展功能(尽管它看起来完全足够了),那是您要查看的地方。我相信,在这种情况下,简短的回答是 window.clear()。


据我所知,

"Graphics" 不是标准的 Python 包。我在 Python 2 和 3 上遇到导入错误。但是,我相信 "Zelle" 图形是一个非常标准的 Python 学习库,而且你看起来像是在学习,所以我猜该图形 == Zelle 图形

也就是说,我会更笼统地回答这个问题,而不是针对那个特定的包,它并没有真正用于规范 Python。

图形建立在 tkinter 之上,tkinter 是 Python 和 tk 之间非常强大的低级接口。 Graphics canvas 实际上是一个 tkinter Canvas 实例。

这是关于 tkinter canvas 对象的 effbot 文档:

http://effbot.org/tkinterbook/canvas.htm

它非常清晰易用 - 您会注意到

my_canvas.delete('all')
my_canvas.create_text(0,0,text='whatever')

解决了您的问题。

去Zelle图形的源代码,很简单,看看canvas隐藏在哪里。然后直接在上面调用方法。它将为您提供更强大的功能和更可扩展的解决方案,而且让您能够轻松地使用更强大的工具!

这是 Zelle 图形的代码:

http://mcsp.wartburg.edu/zelle/python/graphics.py

您会注意到所有 Graphics 实例都有对 "self.canvas" 的引用。它甚至没有隐藏(在变量名上用“__”前缀表示),所以你可以直接访问 canvas 来做任何你想做的事。为了获得最简单的解决方案,我们可能会注意到您也可以只在单个文本对象上使用 "setText",而不是让多个文本对象相互替换。