Site icon Python GUI

Learn How To Build A Python Module Into A Windows DLL For Use In Python GUI Apps

Sometimes we may need to share the functionalities as DLL and we know, creating a DLL in Delphi is a simple task. We learned how to create Python Module and add methods to it in Delphi. How about making a python module as DLL using Python4Delphi and import this python module in another application? This post will guide you to do that. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo9 Sample App shows how to create a Module, add a routine to that module, Import the module in a python script, and access the added routine. You can find the Demo9 source on GitHub.

Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.

Components used in Python4Delphi Demo9 App: No visual components were added to the library however the following class were instantiated by using PythonEngine.pas

You can find the Python4Delphi Demo9 sample project from the extracted GitHub repository ..\Python4Delphi\Demos\Demo09.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

This Sample contains one library project(demodll) and a VCL application project(Demo09).

Demodll contains a procedure PyInit_demodll which will create an instance of TPythonEngine and TPythonModule. PythonEngine loads the python DLL using the LoadDll procedure. PythonModule named ‘demodll’ will have a method to add 2 integers. See the code below. When building this project, demodll.pyd is created.

[crayon-66065dc3162f2197285177/]

In the VCL Application, this Python Module Dll is imported and executed using the below script. The components like TPythonEngine and TPythonGUIInputOutput were used to execute the script and show the result to the Memo2. Note: This sample is referring to version 3.7 in both Dll and Exe. Ensure your python version matches the code. The Screens and code in this post use the 3.9 version.

[crayon-66065dc3162fd317063526/]
Python4Delphi Demo09

Check some of the tutorials available for Python4Delphi here

Exit mobile version