1

I have a program which displays the randint number and the value of the randint. This works great but I need to close the turtle window without stopping the program because I need to do more commands after the turtle window is closed. Here is the code I have so far... # imports required libraries

import random
import turtle

# creates and configures window

wn = turtle.Screen()
wn.setup(800, 400)
wn.title("Random output")
wn.bgcolor("#3d3d3d")
wn.tracer(0)

# creates text

text = turtle.Turtle()
text.speed(0)
text.color("#ffffff")
text.penup()
text.hideturtle()

rep_Num = 0

for repeats in range(100000):

    wn.update()

    rand_Num = random.randint(0, 100)

    rep_Num += 1

    text.clear()
    text.write(str(rep_Num) + ": " + str(rand_Num), align="center", font=("Comic Sans MS", 36, 
"normal"))

# Its here I need to close the turtle window

That's all my code so far. Please help.

Harrison Pugh
  • 113
  • 1
  • 9
  • 3
    Does this answer your question? [How to close the Python turtle window after it does its code?](https://stackoverflow.com/questions/36826570/how-to-close-the-python-turtle-window-after-it-does-its-code) – Abdeslem SMAHI Dec 27 '19 at 13:16

1 Answers1

2

You should add this line at the end of your code.

wn.bye()

Here is the full working code.

import random
import turtle

# creates and configures window

wn = turtle.Screen()
wn.setup(800, 400)
wn.title("Random output")
wn.bgcolor("#3d3d3d")
wn.tracer(0)

# creates text

text = turtle.Turtle()
text.speed(0)
text.color("#ffffff")
text.penup()
text.hideturtle()

rep_Num = 0

try:
    for repeats in range(100000):

        wn.update()

        rand_Num = random.randint(0, 100)

        rep_Num += 1

        text.clear()
        text.write(str(rep_Num) + ": " + str(rand_Num), align="center", font=("Comic Sans MS", 36,
    "normal"))
except turtle.Terminator as e:
    print('turtle window closed!')

print('hello~')
wn.bye()
Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24
  • I did it in a similar way using the turtle.Terminator but I added and if statement checking if rep_Num > 100000 and if that was true I just did called turtle.Terminator and the called break to let the program continue – Harrison Pugh Dec 27 '19 at 14:56