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,
dict
,list
,set
, andtuple
. 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.
- threading: This module constructs higher-level threading interfaces on top of the lower level
- Memo1, used for providing the Python Script to execute.
- On Clicking Execute Button the below code executes the python script.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
The script below is executed which will opens a window console, create threads put in a queue and execute one after other.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import threading_test import sys # the following is needed to use the newly allocated console! sys.stdout = sys.stderr= open('CONOUT$', 'wt') try: count = int(sys.argv[1]) except: count = 3 for i in range(count): print ("**** Pass", i) threading_test._test() print ("**** Done.") |
- 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.
1 2 3 4 5 6 7 8 9 10 |
procedure TForm1.PythonInputOutput1SendData(Sender: TObject; const Data: AnsiString); begin {$IFDEF MSWINDOWS} OutputDebugStringA( PAnsiChar(Data) ); {$ENDIF} {$IFDEF LINUX} WriteLn( ErrOutput, Data ); {$ENDIF} end; |
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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition