Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

Quickly Log Python Script Output To The Delphi Debug Log In Your Windows Apps

Sometimes developers need to log the output messages in Delphi for debugging purposes. You might aware this can be achieved by the windows API OutputDebugStringA . How about direct your python output messages to the Delphi Events Log Window? Yes, Python4Delphi has a flexible component PythonInputOutput to redirect your python output to the Delphi Events Log window with less code. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo23 Sample App shows how to create a Python script in Delphi Application which demonstrates creating thread and output the result in the Delphi Event log window. You can find the Demo23 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 Demo23 App:

  • TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
  • TPythonInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component event you can direct your Python Output to the Delphi Event log window.

You can find the Python4Delphi Demo23 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo23.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

  • PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property.
  • It uses a Python Script file threading_test.py where the below list of modules was used.
    • threading: This module constructs higher-level threading interfaces on top of the lower level _thread module. 
    • collections: This module implements specialized container datatypes providing alternatives to Python’s general-purpose built-in containers, dictlistset, and tuple. In this sample, deque is used.
    • time: This module provides various time-related functions.
    • threading_test.py contains, ProducerThread, ConsumerThread, BoundedQueue class within the function _test.
  • Memo1, used for providing the Python Script to execute.
  • On Clicking Execute Button the below code executes the python script.

The script below is executed which will opens a window console, create threads put in a queue and execute one after other.

  • PythonInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script. The OnSendData event helps to send the python output data to the Delphi Event log window using the below code.
demo23 5875273
<strong>Python4Delphi Demo23<strong>

You can customize your Events Debug Output messages background and foreground colors easily to differentiate your output messages with Delphi IDE. Right-click in the Events Tab -> Properties->Event Log->Colors -> Output Debug String-> Select Foreground and Background.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.