netrate
netrate

Reputation: 443

turtle.ontimer() equivalent for Zelle Graphics (python)?

Is there an equivalent way to perform the same function as the TURTLE ontimer using Zelle Graphics and Python?

The ontimer works with Zelle, but it needs the Turtle window to open (which defeats the purpose). I am trying to figure out a way to use the turtle.ontimer but without having to call the turtle.Screen() or any turtle window. OR if there is another way to do it in zelle graphics.py

Upvotes: 0

Views: 64

Answers (1)

cdlane
cdlane

Reputation: 41872

Since GraphWin is a subclass of tkinter Canvas, we can invoke the same Canvas.after() method that Python turtle uses to implement ontimer():

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

Upvotes: 0

Related Questions