CodeDelphiPythonPython GUIWindows

Quickly Learn To Assign Value Between A Delphi Python GUI And Python In This Windows App

Earlier in the Python4Delphi Demo 3 post, we have learned how to create a Python Delphi Variable using the TPythonDelphiVar component, assign some value to it, and showed the value as a string. You can also use Python4Delphi with C++Builder. This post will see some advanced usage of the TPythonDelphiVar component. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo4 Sample App shows how to assign a variable value between Delphi and Python using the TPythonDelphiVar component events. This can be achieved by events that set and gets data as a variant or python object. Internally this component converts a value from Delphi to python and vice versa. You can find the Demo4 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 Demo4 Sample 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.
  • TPythonDelphiVar: Inherited from TEngineClient, used to convert the python variable to the Delphi variable and vice versa. It has methods to set and get value as variant or PyObject. It contains property like Module(TPythonModule internally created by default) where the python variable(TPyVar) is created and later converted to and from the Delphi variant or PyObject.
  • 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.

You can find the Python4Delphi Demo4 sample project from the extracted repository ..Python4DelphiDemosDemo04.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 Is assigned with InitScript which import sys module and prints the Python.Dll version, copyright information to Memo2import sys print(“Python Dll: “, sys.version) print(sys.copyright) print()
  • PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently executing Python script.
  • PythonDelphiVar1 component is intended for demonstrating how to assign value between Delphi(Edit1.Text)->Python Delphi variable(test.Value) and back from (test.Value) -> Edit1.Text using event PythonDelphiVar1GetData and PythonDelphiVar1SetData respectively. The data is passed as variant type here.

 

  • PythonDelphiVar2 component is intended for demonstrating how to get and set python objects using PythonDelphiVar2ExtGetData and PythonDelphiVar2ExtSetData events. A python object is created using the python script and assigned to PythonDelphiVar2 value. class C: def __init__(Self, Arg): Self.Arg = Arg def __str__(Self): return "<C instance contains: " + str(Self.Arg) + ">" print ("Current value of var object is: ", object) object.Value = C("Hello !") print ("New value is:", object) Note: Need to take care of the reference count for python object.
  • Memo1, used for providing the Python Script to execute, and Memo2 for showing the output. Buttons to perform the execution, load script from, save the script to a file, show variable content to file. TEdit1 to assign Text value between Delphi and Python.
  • On Clicking Execute Button the python script is executed.
Python4Delphi Demo4

Check some of the tutorials available for Python4Delphi here

close
Related posts
Learn PythonPythonPython GUIVideos

Learn Python With Transitioning From Developing Software to Developing People by Matthew Knapp Bachman (PyCon 2020) Video

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

Decipher the DelphiVCL.Application

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

Create a Very Basic Empty Form using DelphiVCL.Form

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

An Introduction To DelphiVCL: A Python GUI Builder For Windows

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

fr_FRFrench