Site icon Python GUI

Learn How To Use Python Code Introspection With A Delphi Windows GUI App

screen shot 2021 01 23 at 5 34 40 pm

The strength of Delphi combined with Python gives its users the chance to fast track their development processes and deliver excellent applications with ease! This tutorial helps you accomplish just that. With Python4Delphi (P4D) you can create a GUI for your python scripts in a few simple steps! P4D is a collection of free components that wrap up the Python DLL into Delphi and C++Builder. This post will demonstrate how we can use Python Lists within the Delphi development environment to save our data and display it in the GUI. For this purpose, our approach will be to create a VCL application and run our script inside of it. The output will be visible as soon as the script is executed.

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

Python4Delphi Demo1 is a Sample App that shows how a Python Script can be run by entering Python code inside of a Memo. The execution and display of results will be conducted inside a separate Memo. The Demo1 Source is available on GitHub at the following link. In order to run the Python script in Python for Delphi, open and run project Demo1. Then insert the script into the lower Memo and click the Execute button. The results will be displayed in the 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 titled “Demo of Python”.

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:

Code introspection is the method of dynamically finding out the type of an object or its nature at runtime. Code introspection in Python is phenomenal. It allows users many different options to inquire more about their python objects, methods or data types. In the following code, we will observe some tools and methods used for Introspection. The fist among them is the method “type” which, as its name suggests, tells you about the type of an object. Observe the code below.

[crayon-6605daae0cb6e357391905/]

The above should return int and string respectively.

Now let’s observe a different function. The dir function returns a list of methods that are associated to the python object in question, In the case of v3, which is a list. We can expect a total of 47. That’s a lot!

[crayon-6605daae0cb78210899782/]

But this does not stop here! There are many more functions available that allow developers to use code introspection! For example, help() is normally used to find what other functions do. Find more about code inspection at the following link!

Check out some more cool tutorials on Python4Delphi over here.

Exit mobile version