TorPy
Vernacular Python Turtle.
- Presentation
- Download and install
- First classroom session
- Some examples
- Operation of TorPy
- Adapt TorPy
- Translate TorPy in another language
Presentation
TorPy is a Frenchified (but which can be translated into any language) version of Python turtle module.
Inspired by the Logo turtle, turtle makes learning to program, since writing instructions that the turtle will not make then.
Furthermore it allows to produce beautiful geometric figures
I wrote TorPy after the turtle usage tests with college students, and its purpose is to overcome the some difficulties.
Some Features of TorPy:
- the instructions of the turtle module are translated into French.
For example is written in avance(100) instead of forward (100) - only instructions are displayed in the input box; the rest of the Python program (shebang, etc) is hidden
- the instructions are executed without needing to be saved before
(but you can of course save the file) - the list of available commands can be accessed at any time; examples of use of each instruction can be copied and pasted
- other Python command can be used for more advanced use (loops, function definitions, module imports, etc). See paragraph operation of TorPy
- the program can be easily adapted to other languages than French and it is also easy to add new instructions. See paragraphs adapt TorPy and translate TorPy in another language
- if the file is saved, the result can be saved as image (EPS format, but also SVG on GNU/Linux if pstoedit is installed).
TorPy is a cross-platform software, free (GNU GPL 3 license), developed in Python (programming language) and PyQt (or PySide) for the GUI.
Download and install
To run, TorPy needs
GNU/Linux
- Dependencies
- Python is obviously already installed
- additional dependencies are PyQt (4 or 5 version), Tkinter and pstoedit
For Ubuntu, this gives one of the following installation commands:- for Python 2.x and PyQt4:
sudo apt install python-qt4 python-tk pstoedit
- for Python 3.x and PyQt4:
sudo apt install python3-qt4 python3-tk pstoedit
- for Python 3.x and PyQt5:
sudo apt install python3-pyqt5 python3-pyqt5.qtwebengine python3-pyqt5.qtsvg python3-tk pstoedit
- for Python 2.x and PyQt4:
- TorPy
- download and unpack the TorPy archive: torpy.tar.gz
- to launch TorPy, just click on the file torpy.pyw at the root of the folder (or console:
python torpy.pyw
) - on the other hand, the menu File → Create Launcher will allow you to create a file torpy.desktop wherever you want (desktop, menu …).
Windows
- Dependencies
- to install Python 3 and PyQt5, see page install PyQt on Windows
- TorPy
- download and install torpy_setup.exe (TorPy installation)
- to launch the program, you will find a shortcut in the Start menu and on the desktop
- Notes
Mac OS X
- Dependencies
- to install Python 3 and PyQt5, see page install PyQt on Mac
- TorPy
- download and unpack the TorPy archive: torpy.tar.gz
- to launch TorPy, it is enough
- click on the file torpy.pyw at the root of the folder torpy
- OR run in console with the command:
python3 torpy.pyw
- OR move the torpy.pyw file to the terminal icon in the dock (at the bottom of the screen)
- Notes
- if this does not work you have to manually modify the application that will open torpy.pyw.
To do this, select the file torpy.pyw then in the file menu, click get information.
In the open with section, select Terminal as the default application (or python launcher).
See this page: How can I change what program opens a certain file on a Mac?
- if this does not work you have to manually modify the application that will open torpy.pyw.
Sources
- development at GitLab.
First classroom session
explanations Python + Turtle
- program is like someone indicate its way: first we give all instructions.
We will program what will make Turtle - instructions should be given in a language that Turtle understand.
The programming language used is called Python.
- program is like someone indicate its way: first we give all instructions.
we launch and click on the button
- why does Turtle do nothing?
- to close the window you can click inside
first instructions (indicated) avance(200) tourneAGauche(90)
complete alone the rectangle of dimensions 200×150
focus on a few things
- copy and paste rather than writing everything
- interest of syntax highlighting
- list of instructions and short versions (eg av instead of avance)
2 squares opposite with a vertex
an equilateral triangle
6° and 5°: a simple house (square topped with an equilateral triangle)
from the 4°: home without lifting the pencil (need to calculate the diagonal)
Some examples
The versions below are not those expected of students (using functions, variables).
home without lifting the pencil
def maison(debut=(0, 0), cote=100, delai=0):
va(debut[0], debut[1])
for i in range(4):
av(cote)
attendre(delai)
tg(90)
attendre(delai)
diagonale = cote * racine(2)
tg(45)
attendre(delai)
avance(diagonale)
attendre(delai)
tg(90)
attendre(delai)
avance(diagonale / 2)
attendre(delai)
tg(90)
attendre(delai)
avance(diagonale / 2)
attendre(delai)
tg(90)
attendre(delai)
avance(diagonale)
attendre(delai)
dimensions = 200
depart = -dimensions / 2
#maison(debut=(depart, depart), cote=dimensions)
maison(debut=(depart, depart), cote=dimensions, delai=0.5)
depart = -100 - dimensions / 2
va(depart, depart)
couleur('rouge')
ecris("C'EST LA MAISON DE LA TORTUE...", 16)
va(0, 0)
couleur('noir')
3 rosaces
def rosace(rayon=100):
for i in range(6):
couleur()
cercle(rayon)
tg(60)
rosace()
rosace(50)
rosace(25)
couleur("noir")
10 squares
def carre(c) :
for i in range(4) :
av(c)
tg(90)
for i in range(10) :
vaEn(10*i-200, 10*i-200)
couleur()
carre((10-i)*30)
vaEn(-50, 150)
couleur("bleu")
ecris("Voilà c'est fait", 20)
vaEn(-50, 200)
Vasarely
from http://www.prpig.org/2014/08/victor-vasarely-meets-tina-turtle.html
def carre(debut=(0, 0), cote=100):
# un carré plein
va(debut[0], debut[1])
turtle.begin_fill()
for i in range(4):
av(cote)
tg(90)
turtle.end_fill()
# nombre de carrés :
nb = 8
# leur taille :
taille = 40
moitie = (nb + 1) / 2
position = [-moitie * taille, moitie * taille]
va(position[0], position[1])
for x in range(nb):
for y in range(nb):
couleur()
carre(position, taille)
couleur()
carre(
(position[0] + taille/4, position[1] + taille/4),
taille/2)
position[0] += taille
va(position[0], position[1])
position[0] = -moitie * taille
position[1] -= taille
va(position[0], position[1])
position[0] -= taille
va(position[0], position[1])
ecris("Alphabet Plastique, de Victor VASARELY.", 20)
position[1] -= taille
va(position[0], position[1])
Operation of TorPy
The launch of TorPy a torpy subfolder is created in the temporary folder of the computer and 3 files are copied from the subfolder files installing TorPy:
- turtle.cfg : configuration file of the turtle window (size, title, etc.)
- importturtle.py : file containing the functions translations.
To use TorPy in languages other than French, the file located in the files subfolder is called importturtle_xx.py, where xx is the language of the computer.
Language is detected at the launch and if the corresponding importturtle_xx.py file exists, it is copied to the temporary folder under the importturtle.py name.
if the file importturtle_xx.py does not exist, importturtle_fr.py is copied - torpy.py : file to be executed (and contains import of importturtle.py file).
When the user requests the execution of his instructions, they are inserted into the torpy.py file before it is executed.
Specifically, the torpy.py file contains the following line that serves as a benchmark and will be replaced by the user instruction:
# USER INSTRUCTIONS
Adapt TorPy
As an example, I will add them begin_fill and end_fill functions from the turtle module, translating them into ** debutDeRemplissage ** and ** finDeRemplissage , with short versions remplir** and finRemplir.
Changes to the file files/importturtle_fr.py
at the beginning of the file, I added 4 new keywords to the keywords list so that they are taken into account by the syntax highlighting:
keywords = ( … ‘debutDeRemplissage’, ‘remplir’, ‘finDeRemplissage’, ‘finRemplir’, )
and further I defined 4 new functions:
def debutDeRemplissage(): turtle.begin_fill()
def remplir(): turtle.begin_fill()
def finDeRemplissage(): turtle.end_fill()
def finRemplir(): turtle.end_fill()
Changes to the file translations/instructions_fr
I add explanations and examples of using in the file:
# pour remplir un dessin (un cercle en exemple) : debutDeRemplissage() cercle(100) finDeRemplissage()
# variante : remplir() cercle(100) finRemplir()
Notes
- in writing this part of the page, I added these 4 functions. So they are already integrated into TorPy
- functions can be added from any Python module, provided it is imported at the beginning of the file.
For example, the trigonometric functions are imported from Python math module - since Python 3 function names (and variables) can be accentuated.
It may be convenient but is avoided because TorPy no longer work with Python 2.
Translate TorPy in another language
Language is detected at launch but can also be passed as parameter.
For example, if TORPY_DIR means the installation path of TorPy:
python TORPY_DIR/torpy.pyw LANG=es
python TORPY_DIR/torpy.pyw LANG=fr_CA
The language is indicated as xx or xx_YY shape.
For example fr show the French, but fr_CA the French Quebec.
If you translate TorPy in another language, please send me (pascal.peter at free.fr) your translation so it can be added to the software. Send me the files are (see below for descriptions):
- importturtle_xx_YY.py
- instructions_xx_YY
- torpy_xx_YY.ts
- and possibly CREDITS_xx_YY.html and README_xx_YY.html
Translation of the functions and instructions
Just change the two following text files and save them by adding xx or xx_YY termination:
- files/importturtle_fr.py → files/importturtle_xx.py or files/importturtle_xx_YY.py
- translations/instructions_fr → translations/instructions_xx or translations/instructions_xx_YY
For example, I created a variant of the French language called fr_LOL.
In importturtle_fr_LOL.py and instructions_fr_LOL files, I just replaced avance by avanssss and ecris by ekri.
Note: since Python 3 function names (and variables) can be accentuated.
That remains to be avoided because TorPy no longer work with Python 2.
However, for some languages this possibility can be very useful.
Translation Interface Torpy (menus, etc)
Just change the following text file and save it by adding the ending xx or xx_YY:
- translations/torpy_fr.ts → translations/torpy_xx.ts ou translations/torpy_xx_YY.ts
The file can be modified with Qt Linguist, which will also allow you to generate a torpy_xx_YY.qm file and test your translation. To do this, ask File → Release in Qt Linguist.
Here is the result by launching TorPy with LANG=fr_LOL parameter:
Eventually translating README and CREDITS
They are displayed in the About window
- translations/CREDITS.html → translations/CREDITS_xx.html ou translations/CREDITS_xx_YY.html
- translations/README.html → translations/README_xx.html ou translations/README_xx_YY.html
Last modification of this page: 2021-02-18.