【问题标题】:Clearing Graphics from a Window in Python在 Python 中从窗口中清除图形
【发布时间】:2015-02-26 02:19:24
【问题描述】:

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

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

【问题讨论】:

  • 您是指“Zelle Graphics”包吗?我不认为 Python dsitributions 中包含标准的“图形”包(至少我得到一个导入错误......)

标签: python text graphics window draw


【解决方案1】:

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


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

也就是说,我将更笼统地回答这个问题,而不是针对特定包,因为规范 Python 中并没有真正使用它。

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

这是关于 tkinter 画布对象的 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”。它甚至没有被隐藏(在变量名上用“__”前缀表示),所以你可以直接访问画布来做任何你想做的事情。为了获得最简单的解决方案,我们可能会注意到您也可以只在单个文本对象上使用“setText”,而不是让多个它们相互替换。

【讨论】:

  • 我不确定 zelle 图形是什么,但是对于我的编码课程,我们必须使用程序 Calico 来运行我们的代码,所以这就是您遇到的问题可能来自的问题。我会看看它,但我们没有得到任何直接评论 setText 或任何类型的画布的资源。我得到了这个页面来引用calicoproject.org/Calico_Graphics#Text,但我认为它没有列出这些命令。
  • 是的,所以 Zelle 图形不是你的菜......我想我应该等你回复评论。 Calico 环境没有在它发布的论文中列出它的实现,或者它的文档(现在甚至还没有列出),但是缓存版本 webcache.googleusercontent.com/… 暗示 window.clear() 可以解决问题
  • 好的,我试试看
  • 谷歌快速搜索发现 Calico 实际上是基于 IronPython 而不是 Cython 构建的,因此它甚至不使用 tkinter。如果您想了解底层实现,它建立在 Gtk 之上。一旦你得到这个答案,我可能会删除它,因为它不准确,你为什么不弄清楚问题所在后自行回答
  • 只是想回来说它有效。我刚刚实现了它,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
相关资源
最近更新 更多