Python tkinter does not finish the drawing - python

i have to draw the house of santa claus with tkinter, but when using the following code it just stops after the first line
First I import turtle and open the gui screen
import turtle as t
t.Screen()
then i use this code
>>> s = 100
>>> points = [(s,0), (s,s), (0,0), (s,s), (s/2.,2.*s), (0,s), (s,0)]
>>> for p in points:
t.goto(p)
t.mainloop()
but it stops after the first line, how do i fix this?

As is specified in the documentation for turtle.mainloop:
turtle.mainloop()
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
Here however it is not the last statement: it is called for each point in points. So you have to rewrite your program to:
for p in points:
t.goto(p)
t.mainloop()
If you read the second boldface part of the specification, it is not even necessary to call it: your program is not interactive. So the following program will be sufficient:
import turtle as t
t.Screen()
for p in points:
t.goto(p)

Related

python turtle graphic doesn't draw

When I run the following code it only pops up a window and doesn't draw any graphic.
I tried a few examples from references but in all cases, it happened. Can someone help me to fix the problem?
import turtle
turtle.mainloop()
t = turtle.Turtle()
t.color('red')
t.pensize(10)
t.shape('turtle')
See here:
https://docs.python.org/3.1/library/turtle.html#turtle.mainloop
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.
So you have the second line to the end of your program
Also without mainloop i can see an red Turtle drawn when run in repl.it:
https://repl.it/#CarstenEggers/Raphael
The reason it's not doing anything is because you haven't actually told the "draw-er" t to draw anything. All the stuff you've told it to do is just set-up instructions: what color to use, what size to draw at, etc.
Try running this code instead. Explanations in comments:
import turtle
# turtle.mainloop() # Generally not necessary to run mainloop; can just delete
t = turtle.Turtle() # Creating a turtle to do drawing
t.color('red') # Telling it what color to draw
t.pensize(10) # Telling it what size to draw at
t.shape('turtle') # What shape turtle draw-er should be: "arrow", "turtle", etc
# Now let's draw something!
t.forward(50) # Tell turtle to draw in forward direction 50 pixels
t.left(80) # Tell turtle to turn in-place 80 degrees to left
t.forward(100) # Draw 100 pixels forward
t.right(90) # Turn in-place 90 degrees right
t.forward(170) # Draw 170 pixels forward
# Done drawing!
turtle.exitonclick() # Tell program to keep picture on screen, exit on click
# Note: See how that's `turtle` and not `t`? That's because we don't want to
# tell our draw-er `t` anything: `t` is just for drawing, it doesn't control
# the larger scope of starting and exiting. If we said `t.exitonclick()`, the
# program would just break at the end, because the draw-er does not know how
# to exit or anything.
# On the the other hand, the module `turtle` (where we get the draw-er and
# everything else from) does know how to handle starting and exiting, so that's
# why we make the call to the module itself instead of the draw-er `t`.

No screen when using turtle in python

I am trying to draw a square using turtle in python, however, it's not opening any screen.
There are no errors in the shell. I am using python 2.7.14 on windows 10
import turtle
def draw_square(some_turtle):
for i in range(1,5):
some_turtle.forward(100)
some_turtle.right(90)
def draw_art ():
window = turtle.Screen()
window.bgcolor("green")
fahim = turtle.Turtle()
fahim.shape("turtle")
fahim.color("yellow")
fahim.speed(2)
for i in range(1,37):
draw_square(fahim)
fahim.right(10)
window.exitonclick()
You've defined the functions, but you haven't called them.
Functions only run when they're called, and as you haven't called them, no animation will be displayed. This means that you need to call your functions o get the animation.
Just add draw_Art() to the end of your code and there should be an animation.

Re-open turtle after turtle.bye()

I have some code as follows:
# My code here
turtle.bye()
After that, is there any way I can reopen the turtle window.
I know you can do turtle.clearscreen() but that does not close the turtle window.
I will accept any answer which allows me to close the turtle graphics window and then reopen it without opening and running another python program to do this.
Thank you in advance
I've seen situations where the approach of #LukeTimmons worked but not always reliably and not in every situation. Give this solution a try:
import time
import turtle
turtle.dot(200, 'green')
time.sleep(2)
turtle.bye()
# These two lines (indirectly) resurrect turtle environment after turtle.bye()
turtle.Turtle._screen = None # force recreation of singleton Screen object
turtle.TurtleScreen._RUNNING = True # only set upon TurtleScreen() definition
turtle.dot(200, 'red')
turtle.mainloop()
It resets two flags that keep turtle from starting up again. It may be safer to create your own turtle after restart rather than use the default turtle which may point back to the departed environment.
There may be other ways but this is the only way I know.
from turtle import *
def turtle1():
#Your code here
turtle1()
turtle.bye()
turtle1()
This should re-run your code without re-typing it.

My turtle program in python acts like it has been clicked

import turtle
window = turtle.Screen()
pen = turtle.Turtle()
def star():
pen.forward(100);
turtle.onscreenclick(star())
window.mainloop()
I am very new to turtle,and python itself, I am just using some of the documentation that I found on the python website, but when I run this program it runs my function star without the screen even being clicked. The end goal of this program would be for me to click then it would run the function, then if I clicked again the screen would clear and the function would be run again.
Thanks for the help!
You need to pass reference to start function into onscreenclick, and make start to accept two arguments:
def star(x,y):
pen.forward(100);
turtle.onscreenclick(star)

How to close the Python turtle window after it does its code?

I'm working on a simple program in Python 3.5 that contains turtle graphics
and I have a problem: after the turtle work is finished the user has to close the window manually.
Is there any way to program the window to close after the turtle work is done?
turtle.bye(), aka turtle.Screen().bye(), closes a turtle graphics window.
Usually, a lack of turtle.mainloop(), or one of its variants, will cause the window to close because the program will exit, closing everything. turtle.mainloop() should be the last statement executed in a turtle graphics program unless the script is run from within Python IDLE -n which disables turtle.mainloop() and variants.
turtle.Screen().mainloop() and turtle.done() are variants of turtle.mainloop().
turtle.exitonclick() aka turtle.Screen().exitonclick() binds the screen click event to do a turtle.bye() and then invokes turtle.mainloop()
Try exitonclick() or done() at the end of the file to close the window .
Add tkinter.mainloop()at the end of the file.
example
import turtle
import tkinter as TK
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(90)
TK.mainloop()

Categories