有没有一种等效的方法可以使用 Zelle Graphics 和 Python 来执行与 TURTLE 准时器相同的功能?
ontimer 与 Zelle 一起工作,但它需要打开 Turtle 窗口(这违背了目的)。我试图找出一种使用turtle.ontimer 的方法,但不必调用turtle.Screen() 或任何turtle 窗口。或者如果在 zelle graphics.py 中有另一种方法可以做到这一点
回答1
由于 GraphWin
是 tkinter Canvas
的子类,我们可以调用 Python turtle 用来实现 ontimer()
的相同 Canvas.after()
方法:
from graphics import GraphWin, Circle, Rectangle, Point
def change(a, b):
a.undraw()
b.draw(window)
window.after(1000, change, b, a) # repeat one second later
window = GraphWin("Shape Shifting", 100, 100)
circle = Circle(Point(50, 50), 20)
circle.setFill('red')
circle.draw(window)
square = Rectangle(Point(30, 30), Point(70, 70))
square.setFill('green')
change(circle, square)
window.getMouse() # Pause to view result
window.close() # Close window when done