DelphiPythonPython GUIWindows

How To Make A GUI For Learning Python Using Turtle Library

blogbanner3-60

Are you looking for an open-source library for a fun educational environment for learning Python, and create a GUI for that? 

This post will get you to understand how to use the Turtle Python Library using Python4Delphi (P4D) in the Delphi/C++ Builder application and create a nice GUI for it. 

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’.

 

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.

0_rundemo1-6813476

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:

Here is the output in Python4Delphi GUI:

1-8967767

 

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?

Here is the output in Python4Delphi GUI:

2-3247119

Next example, let’s draw colorful spirals using the following script:

3-5661662

 

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

close
Related posts
DelphiLearn PythonPythonPython GUIRAD StudioWindows

How To Make Apps With Advanced Natural Language Processing

DelphiPythonPython GUIWindows

This Is How To Automatically Parse RSS Feeds In Your Apps

Learn PythonPythonPython GUIVideos

Create Your First GUI With Python Using 5 Lines of Code

Learn PythonPythonPython GUIVideos

How To Make A Standalone Python EXE Executable

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish