Python turtle.done() alternatives - how do they differ? - python

I've seen python turtle programs use turtle.done(), turtle.mainloop() and turtle.exitonclick() apparently interchangeably. The docs give an example where they just use done() (which would be turtle.done() with import turtle.
Is there a reason to ever use anything but turtle.done(), which is my preferred command?

The short answer is that turtle.done() is an alias for turtle.mainloop() so the two are identical. The turtle.exitonclick() does what turtle.mainloop() does, but adds an event handler (the program exits when you click anywhere on the window.)
For more detail, see these specific answers to these questions:
How to close the Python turtle window after it does its code?
Python: How to reset the turtle graphics window
There is some subtlety in choice when moving between Python 2 and Python 3 as some of these change from functions to methods.

Related

Why is there a problem with Turtle module?

My turtle module does not work in IDE. Can anyone help me?
Code:
import turtle
t = turtle.pen()
I wrote this code to make the turtle module open its app, but it didn't happen.
You have to add turtle.mainloop() or turtle.done() at the last of the file.
Starts event loop - calling Tkinter’s mainloop function. Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics.
You can read turtle document for more details.

Turtle Graphic Window not working from VS Code

I am using Visual Studio Code as my IDE and I am a bit of a beginner with Python so I decided to try out the turtle library built into Python to learn some of the syntax. However, when I tried running just a simple script to see if it would work, the window flashed open for less than a second then closed. I have tried using different extensions and re-downloading the python extension for VS Code. This is my code I'm trying to run:
import turtle
geoff = turtle.Turtle()
geoff.forward(100)
Please help as I really can't figure out why the window won't stay open. Thanks!
The screen flashed on and then closed because when the application is done, Python exits and thus so does the screen. This has nothing to do with VS Code or the Python extension and simply how applications work.
Probably the easiest way to keep the window open is add the following line at the very end:
input("Press any key to exit ...")
That way Python won't exit until you press a key in the terminal.
The easiest solution is to add the following line in your V.S. Code:-
turtle.done()
This would prevent the window (Python Turtle Graphics) from closing after running the code:)
You can use exitonclick() to avoid the window from shutting down.
import turtle
window = turtle.Screen()
geoff = turtle.Turtle()
geoff.forward(100)
window.exitonclick()
This way, the graphics window will shut only after you click.
You can create a canvas into turtle like a blank space to draw on. Use this code just to import the module an hold on the graphic window open -Pen It will work with Visual Studio Code, Spyder or Python IDLE
import turtle
window = turtle.Screen()
geoff = turtle.Turtle()
t = turtle.Pen()
window.exitonclick()

Using write() in Python and Python turtle

I want to use an input parameter in my Python code, but instead of being asked in the Python editor toolbox, the user should be prompted in the Python Turtle Graphics window. I believe that this should be done by built-in write(), but I have no idea how to implement it.
"Write" is used for the turtle to write something in the window. You can not currently have an input in the turtle window, unless you code it all yourself.

Difference between turtle.listen() and screen.listen()

Is there any difference between window.listen(), screen.listen(), and turtle.listen()?
I see them used differently in various programs but assume they can be called interchangeably.
I suggest you read Difference between turtle and Turtle?. The short answer is that the turtle library provides two API's, a function-based one and an object-oriented one. (Behind the scene, the function one is built atop the object one at load time.) The confusion begins when you mix the two. My answer in the link explains one way to avoid doing so.
turtle and screen have separate inputs.
For example:
turtle.onclick() and screen.onclick() may seem the same but screen.onclick() is referring to the general window while turtle.onclick() is referring to the turtle module itself.
Turtle
When calling turtle.onclick() you are activating the (onclick) function, to call your argument function whenever the user clicks specifically on the turtle object.
Screen
When calling screen.onclick() you are activating the (onclick) function, to call your argument function whenever the user clicks anywhere on the window.
This is equivalent to turtle.onscreenclick() because onscreenclick() refers to the entire screen. Hence the name screenclick rather than just click which refers to the turtle object.
Listen
So because turtle and screen have separate input functions, you'll need separate listening functions.
So
turtle.listen() listens for overall the entire turtle module's inputs, whilst screen.listen() listens for screen / window inputs.

Turtle Graphics Python, .mainloop()

I am programming in Python and I have a few questions that I can't find the answer to anywhere (please read all questions as they build up to my last question):
1.What Does the .mainloop() really do? I read the all the answers in Stack Overflow, I also checked the documentations explanation.
2.Does the .mainloop() always have to be at the end of a turtle program?
3.I have used .mainloop() before. My question is, if I have the f.f.g code:
import turtle
screen = turtle.Screen()
alex = turtle.Turtle()
tess = turtle.Turtle()
def yes(x, y):
alex.onclick(yes)
print("Hello World")
tess.onclick(yes)
turtle.mainloop()
Why does alex get an action event when the function yes() is run? I know it does because the function is called, but what is actually happening? I mean the statement turtle.mainloop() is run before tess is clicked, and tess's action event is waited for in the event loop, so how does alex's event get in the event loop since its statement is run after turtle.mainloop() is run?
1.What Does the .mainloop() really do?
Turtle's mainloop() calls tkinter's mainloop() which calls Tk's Tk_MainLoop() which is surprisingly simple:
void
Tk_MainLoop(void)
{
while (Tk_GetNumMainWindows() > 0) {
Tcl_DoOneEvent(0);
}
}
It handles events, one at a time, in an infinite loop while there are any main windows open. The events processed include keyboard input, button clicks, window reshapes, file I/O, network activity, timers, display refreshes and any other registered callbacks.
An excellent, one page description of mainloop can be found in the introduction to Chapter 15. Anatomy of the MainLoop in the O'Reily book Mastering Perl/Tk by Steve Lidie and Nancy Walsh. Although it's a Perl/Tk book, the information regarding mainloop is valid for Python as well. You can find this material on-line but I won't include a link here as I don't know which, if any, of the online copies are legitimately posted. But you've enough information to search for it.
2.Does the .mainloop() always have to be at the end of a turtle program?
No. It should be part of a well designed program but isn't required. Most standalone programs will include it (or something that calls it like .done() or .exitonclick()) as the graphics window will close on completion without it. But some situations, eg. IDLE perhaps, don't need it to keep the graphics visible. A common error I find in beginner's turtle programs is creating an infinite loop of turtle activity ahead of calling mainloop() and then wondering why various events don't fire.
If you plan to have Tk process keyboard, mouse and timer events for you, then calling .mainloop() is how you get that started. In most Python/Tk programs, it's the last statement but there can be other code after it that gets executed when all the Tk windows have all closed down.
We can think of turtle programming as writing plug-in code for Tk's main loop. After we set things up, subsequent activity will be done by call back functions we've registered via on*() functions.
3.I have used mainloop() before. My question is, if I have the f.f.g code: ... Why does alex get an action event when the function
yes() is run
When your program runs, turtles Alex and Tess are piled atop each other in the center of the window. When you click on this turtle stack, the event goes to Tess, who's both on top and has an event handler. In her event handler, Tess installs an event handler on Alex. The act of installing an event handler on Alex causes Alex to move in front of Tess, rising to the top of the stack. From now on, when you click on the turtle stack, Alex handles the events and they no longer reach Tess. We can see this clearly if we give them different colors and different event handlers:
import turtle
alex = turtle.Turtle(shape="turtle")
alex.color("blue")
tess = turtle.Turtle(shape="turtle")
tess.color("pink")
def tess_handler(x, y):
alex.onclick(alex_handler)
print("Tess clicked")
def alex_handler(x, y):
print("Alex clicked")
tess.onclick(tess_handler)
turtle.mainloop()
Clicking on the turtle stack produces:
> python3 test.py
Tess clicked
Alex clicked
Alex clicked
Alex clicked
Alex clicked
Alex clicked
Alex clicked
You could move Alex and Tess to different locations in the window and then click on them to confirm that Alex doesn't start receiving events until the first time Tess is clicked.
So mainloop() is an infinite loop that basically blocks the execution of your code at a certain point. You call it once (and only once).
so lets say:
while true:
circle.draw()
sumden.mainloop()
print "circle is being drawn"
time.sleep(0.1)
You will never see the output and print statement because there is no loop.

Categories

Resources