Hello
I am trying to execute a simple code with turtle.
from turtle import *
def marche(h):
left(90)
forward(h)
right(90)
forward(h)
def escalier(n, h):
for k in range(n):
marche(h)
speed(5) #parametrage de la vitesse de 1 lent à 10 rapide, 0 étant la vitesse la plus rapide
shape("turtle") #choix de la forme de la tortue
pencolor("red") #choix de la couleur du crayon
pensize(4) #épaisseur du crayon
up() #lever le crayon
goto(-150,-150) # aller à la position (-200,-200)
setheading( 0) #orientation de la tortue vers l'Est / 90 Nord / 180 Ouest / 270 Sud
down() #poser le crayon
escalier(5, 50)
exitonclick() #indispensable en cas d'exécution dans un IDE comme Pyzo
This code works as expected in a terminal directly with python3 (on a mac os) or also with a venv.
But it does not works in PyCharm.
The windows “Python Turtle Graphic” open but nothing append then
no warning or error in the consol of pycharm
I am new to pycharm I miss something… but what?
Thanks for help
J’ai tenté de tester tes 3 conseil sans trop de succes…
1- l’ajout de done() ne change rien
2 - je n’ai apparemment pas de “mode Scientific” et j’ai cru voir sur le net que c’etait un plugin pour PyCharm Pro?
3 - Pour le Run in terminal, j’ai testé avec “Run with Python console” ou “Emulate terminal in output console” => Pas mieux…
Oups et avec la reponse a ta question :
Avant je travaillais sous vi sur une fenêtre terminal…
Je viens de passer a PyCharm et j’ai le fichier main.py ouvert et je lance l’execution avec le triangle vert
ni update() ni mainllop() ne change rien.
Je ne crois pas que c’est le prog qu’il faut modifier.
car comme dit avant le programme marche tres bien si je le lance depuis une fenetre Term avec
my-venv/bin/python main.py
Il doit y avoir un problème avec ma conf de pycharm
La fenetre Turtle s’affiche mais sans rien dedans ni les “shape” ni le “pencolor”
Je vais regarder comment le debugger marche car j’ai l’impression que cela se bloque au milieu du programme
I tested your script using the PyCharm terminal. It works for me if I rename the module other than turtle.py and if I import the turtle module as an alias. I named the test script tut.py.
Here is the script:
import os
os.chdir(r'C:\Desktop') # since I saved the module to my Desktop
##from turtle import *
import turtle as tt
def marche(h):
tt.left(90)
tt.forward(h)
tt.right(90)
tt.forward(h)
def escalier(n, h):
for k in range(n):
marche(h)
tt.speed(5) # parametrage de la vitesse de 1 lent à 10 rapide, 0 étant la vitesse la plus rapide
tt.shape("turtle") # choix de la forme de la tortue
tt.pencolor("red") # choix de la couleur du crayon
tt.pensize(4) # épaisseur du crayon
tt.up() # lever le crayon
tt.goto(-150, -150) # aller à la position (-200,-200)
tt.setheading(0) # orientation de la tortue vers l'Est / 90 Nord / 180 Ouest / 270 Sud
tt.down() # poser le crayon
escalier(5, 50)
tt.exitonclick()
In the PyCharm terminal, I then enter:
py tut.py
… and I observe a window open up with the turtle climbing a red stair like profile to the right.
I have also put your modified code in a new project in PyCharm (file main.py)
(I have removed the import os and the chdir to desktop)
when trying to launch it in the terminal with
python main.py
I get the same behaviour, the tutrle windows open but is empty.
in the Term I get a message
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don’t rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
but it seem’s when reading that is is not a probleme
I program is stuks in the “first” tt.xxx lines
with the prog like this it sucks at the tt.pencolor but if I put pencolor befor shape it stucs at shape…
I definitively think it is not by changing something in the program that it will works…
My PyCharm seem’s to be not well configure, or has a compatibility issue with turtle?
Ok, you can remove or comment out the import os and the os.chdir(r'C:\Desktop') in the python module.
Can you save the python script to the Desktop just for this test please so that we are on the same page. Then, in the terminal window, change the working directory to C:\Desktop via this command: chdir C:\Desktop.
When you run the script, then type:
py tut.py # or whatever name you have given your script
It should run just fine. Btw, I am using Python v3.13 for my interpreter in Pycharm if that makes a difference.
That is because the green triangle ONLY runs what is in an active Pycharm module. This is normal and to be expected.
So, the terminal runs files that are explicitly stated via the py my_file.py command assuming that you are in the working directory … the directory where you have the file stored (or if not in the active working directory, you would have to include its path along with its module name). If you open the tut.py inPycharm window, then when pressing the green arrow will run the script module.
For example, in the following image, I have two script modules that I have opened in PyCharm.
If I hit the green arrow on top, it will run the currently active Python module.
If you open your tut.py file in PyCharm, and you see its name on the above tabs, and click on the green arrow on top, it will run. Remember, the terminal is a substitue for the CMD terminal - separate from PyCharm. In other words, it is a tool of convenience that has been conveniently integrated into the PyCharm IDE environment so that you do not have to open a separate CMD window. It is technically NOTPyCharm.
Btw, if you already have an active script module opened in PyCharm, you can comment out the entire code above and then paste the tut.py code at the bottom. If you hit the green button, it should run. I use Windows. If I select code and press ctrl + /, it comments out the code. You can try to see if it is the same on a mac. If not, in the PyCharm IDE, go to the top menu and look for Code menu, and look for // Comment with Line Comment for keyboard short-cut.
Can you download the latest version from here (v3.13.3):
Once having done so, in PyCharm, go to: Settings → Python Interpreter (see image from previous post). To the right, it will have the option: Add Interpreter. To add the newly installed interpreter, go to where the interpreter was downloaded to. It might be to here (double check for your specific case):