Python is a powerful, general purpose object oriented language and Delhpi is an amazing platform that allows developers to make visual apps. Combining the power of the two results in the extraordinary features and capabilities of Python4Delphi. This set of free components works by wrapping up the Python DLL into Delphi and C++Builder thus enabling its users to view GUI for their Python scripts with ease. In this tutorial, we will show you just how easy it is to set up your P4D project and start building!
For the purpose stated above, we proceed by making a VCL app. This can be achieved by running the Demo1 project project which will be made available to you in the following gitHub repository link. We will now proceed to explaining how you can run Demo1but before that, let’s take a look at the prerequisites.
Prerequisites: Before we begin working, it is essential we Download the latest version of Python for your platform. You can find the installation instructions for Python4Delphi at this link. Alternatively, you can follow the easy instructions found in this video Getting started with Python4Delphi.
In order to run the Python script in Python for Delphi, open and run project Demo1. Then insert the script into lower Memo, click Execute button, and get the result in upper Memo. If you have successfully completed the prerequisites and downloaded Demo1 on your computer, simply open and run the project Demo1. You will see a small window titles “Demo of Python” This window will have an upper and a lower Memo. Enter your Python script in the lower Memo and click execute. You shall see the results on the upper Memo soon after.
The implementation details of how Delphi manages to run your Python code behind the scenes and which code is executed in order to accomplish that can be found at this link.
The Python Code:
This particular segment will explain the Python code that we will write to demonstrate how our Demo1 VCL application works. Since our focus in this tutorial is the explanation of basic operators, let’s take a look at the arithmetic operators first.
#Lets create two variables
v1 = 2
v2 = 7
v3 = v1 + v2
print("Addition : ", v3)
print("Subtraction : ", v2-v1)
v4 = v1*v2
print("Multiplication : ", v4)
print("Division : ", v2/v1)
So we’re printing out the value of our operators as well as a label to identify them in the GUI. We should be expecting the values (9, 5, 14, and 3.5). Let’s see what we got!
Alright, perfect! We got all the correct answers as we expected in the bottom left window.