In this post, you’ll understand how to use the Turtle Python Library using Python4Delphi (P4D) in the Delphi/C++ Builder application and create a nice GUI for it . Learning more about Python using Turtle Library will allow you to easily build GUIs with Python For Absolute Beginner.
Turtle is an educational environment for learning Python, suitable for beginners, children, or others with zero interest in programming. Turtle library is inspired by LOGO. Shapes, figures, and other pictures are produced on a virtual canvas using the method Python turtle.
The following are common methods used in Python’s Turtle:
- Turtle(): Used to create and return a new turtle object.
- forward(value): For the value specified, the turtle will move forward.
- backward(value): For the value specified, the turtle will move backward.
- right(angle): Clockwise turn of the turtle.
- left(angle): Counterclockwise turn of the turtle.
- penup(): Turtle pen is picked up.
- pendown(): Turtle pen put down.
- up(): Same as penup().
- down(): same as pendown().
- color(color name): Turtle pen’s color is changed.
- fillcolor(color name): Color used to fill a particular shape is changed.
- heading(): Current heading is returned.
- position(): Current position is returned.
- goto(x, y): Moves position of turtle to coordinates x, y.
- end_fill(): Current fill color is filled after closing the polygon.
- begin_fill(): The starting point is remembered for a filled polygon.
- dot(): Dot is left at the current position.
- stamp(): Impression of turtle shape is left at the current position.
- Shape(): Should be – ‘turtle’, ‘classic’, ‘arrow’ or ‘circle’.
Table of Contents
How to run a very basic example of Python’s Turtle?
This post will guide you on how to run the Turtle library using Python for Delphi to display it in the Delphi Windows GUI app.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Here are the steps to run a very basic example of Python Turtle:
- The turtle module is imported.
- A turtle to control is created.
- Methods of turtle are used to play or draw around.
- Run the code using turtle.done().
The following are the code to execute the above steps inside Python4Delphi GUI:
1 2 3 4 5 6 7 8 9 10 |
import turtle Window_ = turtle.Screen() Window_.bgcolor("light green") Window_.title("Turtle") a = turtle.Turtle() a.forward(200) turtle.done() |
Here is the output in Python4Delphi GUI:
How can we draw shapes with the Turtle Library?
One of the powerful things about computer programming is that it can do the boring stuff for us. For example, what if we wanted to draw a hexagon (a shape with 6 sides), or even more sides?
Rather than defining the number of sides each time manually, we can instead define variables and have Python use a formula to find out for us.
Here, we’ve set the number of sides to be 6, and the side length to be 70. What happens if we change the variables?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import turtle polygon = turtle.Turtle() num_sides = 6 side_length = 70 angle = 360.0 / num_sides for i in range(num_sides): polygon.forward(side_length) polygon.right(angle) turtle.done() |
Here is the output in Python4Delphi GUI:
Next example, let’s draw colorful spirals using the following script:
1 2 3 4 5 6 7 8 9 |
from turtle import * colors = ['orange', 'red', 'pink', 'yellow', 'blue', 'green'] for x in range(360): pencolor(colors[x % 6]) width(x / 5 + 1) forward(x) left(20) |
Congratulations, now you have learned how to run the Turtle library using Python for Delphi to display it in the Delphi Windows GUI app.
Check out the Turtle library for Python and use it in your projects: https://pypi.org/project/PythonTurtle/ and check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi