Turtle graphics - How do I control when the window closes?

前端 未结 5 1622
刺人心
刺人心 2020-12-14 01:01

I have a small python script which draws some turtle graphics. When my script has finished running, the turtle screen automatically closes, so to be able to see the graphics

相关标签:
5条回答
  • 2020-12-14 01:21
    import turtle
    
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    # etc.
    
    turtle.getscreen()._root.mainloop()  # <-- run the Tkinter main loop
    

    (edit: turtle.done() as suggested by hua below is less ugly.)

    0 讨论(0)
  • 2020-12-14 01:27

    simply use the mainloop() function imported from turtle's module itself!.

    import turtle
    
    
    #Draw a square
    for i in range(4):
        turtle.forward(200)
        turtle.left(90)
    
    
    #calling for the mainloop()
    turtle.mainloop()
    
    0 讨论(0)
  • 2020-12-14 01:29

    Just use turtle.done() or turtle.Screen().exitonclick() as a last command of your turtle program.

    0 讨论(0)
  • 2020-12-14 01:41

    Try adding input() at the end of your code.

    0 讨论(0)
  • 2020-12-14 01:46

    This waits for several clicks - and draws a spiral while you click - until it decides to exit on the last click:

    import turtle
    
    
    win = turtle.Screen()
    win.bgcolor("white")
    
    tess = turtle.Turtle()
    
    tess.speed(0)
    tess.color("blue")             
    tess.pensize(5)                 
    offSet=30
    
    def doNextEvent(x,y):
    
        global offSet
        global win
        tess.forward(20)
        tess.left(1+offSet)
        offSet=offSet-2
        if(offSet<1):
            win.exitonclick()
    
    
    win.onclick(doNextEvent)
    win.listen()
    win.mainloop()
    
    0 讨论(0)
提交回复
热议问题