Reputation: 503
I'm a beginner just trying to mess around with Python. I wrote some very simple code using the Turtle module, but something I can't figure out is why the GUI closes immediately after it's done drawing?
I've tried turtle.getscreen()._root.mainloop(), and the sleep command (which my cmd doesn't recognize), but to no avail. Any thoughts?
Realize this is a trivial question, but people say the best way to understand things is to get in there and do random things :)
Code ( extracted from comment):
from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
turtle.getscreen()._root.mainloop()
Upvotes: 0
Views: 5642
Reputation: 1
This code seems fine:
from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
Except turtle.getscreen()._root.mainloop()
. First of all, root
is not defined. Second of all, I am pretty sure ._root.mainloop()
does not exist. Try this:
from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
#correction under this
turtle.getscreen()
mainloop()
This code should work, hope this helps!
Upvotes: 0
Reputation: 27
My answer would be to remove root because it's not assigned ,here is my code try it out:
from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
mainloop()
Okay this is maybe too much but you can use a for loop(a loop that repeats for some amount of time):
from turtle import *
setup()
title("turtle test")
clear()
down()
for i in range(3):
forward(50)
right(90)
forward(500)
mainloop()
Python is a great language consider working on learning it!
Upvotes: 0
Reputation: 129
Fix it like this
from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
done()
Upvotes: 0
Reputation: 69190
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'turtle' is not defined
Because you did from turtle import *
you do not have a turtle
module for turtle.getscreen()._root.mainloop()
, generating the error above.
Instead, try mainloop()
.
Upvotes: 2
Reputation: 110561
The screen should not "disappear"- if you are calling the mainloop() method correctly - however, if there is a syntax error in your source code, or other Python exception is raised, the program would finish immediately.
If instead of clicking on your program, you run it from a command terminal, you will see the error traceback.
POst it on your question (along with your code, properly formated, which you can do by clicking on "edit" on the question), so that people may help you further.
(btw, calling the mainloop method in the way you describe is works for me).
Now one thing: the built-in Python Tkinter turtle is mostly a toy, and the fun part is playing along with it in the interactive mode, typing commands to it as you go, not to write a script with it. If you want to do some serious art using a turtle model for driving, you be better writing your own turtle.
Upvotes: 1