DelphiFeeds.com

  • Dashboard
  • Popular Stories
  • Trending Stories
  • Feeds
  • Login
Trending now

Quick Logger Is A Powerful Enterprise-Grade Asynchronous Logger For Delphi

Learn How To Use C++ Atomic Operations For Windows Development In C++Builder

Easily Create Ultra-Fast C++ Applications With Low-Level libsimdpp Library In C++Builder

Delphi JOSE JWT Is A Powerful JSON Web Token Library For Delphi

Learn How Not To Use Square Brackets in Your Program In This Learn Delphi Video

Easily Communicate With The TI Gas Sensor Platform Using Delphi And C++ Builder

Powerful Video Game Collection Curation Software Is Built In Delphi

Rise of Legions Multiplayer RPG Windows Game Built In Delphi

Cross-Platform 4D Solar System Simulation Is Built In Delphi

Flexible Cross-Platform Open-Source Component Suite For Delphi FireMonkey

Quickly And Easily Hook Delphi And Windows API Functions With The DDetours Library

Discover How to Use C++ Alias Templates For Windows Development In C++Builder

QuickLib Is A Powerful Third Party Library For Delphi Which Can Boost Productivity

Discover How to Clone The Classic Minesweeper Game in Delphi in This Learn Delphi Video

TMS VCL UI Pack v10.5 released

Learn About Using Delphi Methods As Python Functions With Python4Delphi Sample App

1
Anbarasan Anbarasan 2 months ago in cbuilder, Code, Delphi, object pascal, programming, Python, python4delphi, TDelphiMethod, TPythonEngine, TPythonModule 0

Earlier we have seen how to add methods in Python Module using Python4Delphi TPythonModule component’s Add Method. However, the Add Method parameter uses a PyCFunction type parameter. In many cases, we may need to define the method which should return Delphi Type. How to do that? This post will provide a way to do that using the TPythonModule component itself. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo7 Sample App shows how to create a Module, add a Delphi Method to that module, Import the module in a python script, and access the added routine. You can find the Demo7 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 Demo5 App:

  • TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
  • TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output.
  • TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddDelphiMethod, AddDelphiMethodWithKW to add a method of TDelphiMethod type. The difference between using AddMethod and AddDelphiMethod is the method parameter type which uses PyCFunction and TDelphiMethod respectively. You can create events using the Events property.
  • TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.

Note: This Demo also uses TPythonType component but this post doesn’t cover that. To learn about TPythonType check this post.

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

Implementation Details:

  • PythonEngine1 component provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property. It Is assigned with InitScript which import sys module and prints the Python.Dll version, copyright information to Memo2 using this InitScript. import sys print(“Python Dll: “, sys.version) print(sys.copyright) print()
  • PythonGUIInputOutput1 component provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script.
  • PythonModule1 with Module name spam is created. During PythonModule1Initialization a method spam_getdouble and spam_getdouble2 is added to the Module. And the definition of the method is included in the same unit file. Logically a python module is created and its methods were created using this PythonModule. Later this can be imported into your python script wherever necessary.
procedure TForm1.PythonModule1Initialization(Sender: TObject);
begin
  // In a module initialization, we just need to add our
  // new methods
  with Sender as TPythonModule do
    begin
      AddDelphiMethod( 'foo',
                       spam_foo,
                       'foo' );
      AddDelphiMethod( 'CreatePoint',
                       spam_CreatePoint,
                       'function CreatePoint'+LF+
                       'Args: x, y'+LF+
                       'Result: a new Point object' );
      AddDelphiMethod( 'getdouble',
                       spam_getdouble,
                       'getdouble' );
      AddDelphiMethod( 'getdouble2',
                       spam_getdouble2,
                       'getdouble2' );
    end;
end;
  • Memo1, used for providing the Python Script which imports spam (PythonModule1), accesses the method spam_getdouble( uses Py_BuildValue) and spam_getdouble2 (uses ArrayToPyTuple) and prints the output in Memo2. Buttons to perform the execution, load script from, and save the script to file.
  • On Clicking Execute Script Button, the below script is executed.
import sys
print ("Version:", sys.version)
import spam
print (spam.foo('hello world', 1))
p = spam.CreatePoint( 10, 25 )
print ("Point:", p)
p.x = 58
print (p.x, p)
p.OffsetBy( 5, 5 )
print (p)
print ("Current value of var test is: ", test)
test.Value = "New value set by Python"
print (spam.getdouble())
print (spam.getdouble2())
Python4Delphi Demo7

Check some of the tutorials available for Python4Delphi here

The post Learn About Using Delphi Methods As Python Functions With Python4Delphi Sample App first appeared on Embarcadero Blogs.

Trending Stories

  • Quick Logger Is A Powerful Enterprise-Grade Asynchronous Logger For Delphi

  • Learn How To Use C++ Atomic Operations For Windows Development...

  • Easily Create Ultra-Fast C++ Applications With Low-Level libsimdpp Library In...

  • Delphi JOSE JWT Is A Powerful JSON Web Token Library...

  • Learn How Not To Use Square Brackets in Your Program...

Embarcadero GetIt

  • ProDelphi 64-bit & 32-bit. Lite version

    Source code profiler for measuring runtime of 64 and 32 bit applications developed with Delphi. […]

  • TChromeTabs

    TChrome tabs is a comprehensive implementation of Google Chrome's tab system - features include - […]

  • ICS for FMX and VCL

    ICS is a Delphi library composed of many internet components supporting all major protocols and […]

  • ICS for VCL

    ICS is a Delphi library composed of many internet components supporting all major protocols and […]

  • SynEdit for VCL

    SynEdit for Delphi and CBuilder. Syntax highlighting edit control, not based on the Windows common […]

  • Learn Delphi
  • Learn C++
  • Embarcadero Blogs
  • BeginEnd.net
  • Python GUI
  • Firebird News
  • Torry’s Delphi Pages
Copyright DelphiFeeds.com 2021. All Rights Reserved
Embarcadero
Login Register

Login

Lost Password

Register

Lost Password