CodeDelphiPythonPython GUIWindows

Create Flexible Python GUI Delphi Variables In This Windows GUI App

As we know, a Variable stores a value so that you can reuse it later in your program. This reduces redundancy, improves performance, and makes your code more readable. What is Python Delphi Variable? This post will guide you to understand. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo3 Sample App shows how to create a Python Delphi Variable in Delphi using the TPythonDelphiVar component(convert the python variable to the Delphi variable and vice versa), shows the variable type and the value assigned to it, reassign different type value to the same variable, show the variable content in a message. You can find the Demo3 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. Check Demo1 and Demo2 which covers how to run a python script and evaluate an expression in Delphi.

Components used in Python4Delphi Demo3 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.
  • 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 Demo3 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo03.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 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.

In Python, variables need not be declared or defined in advance, as is the case in Delphi. To create a variable, you just assign it a value and then start using it. A variable may be assigned a value of one type and then later re-assigned a value of a different type.

  • PythonDelphiVar1 created with the name ‘test’. This variable is used in the Memo1 default python script, which is assigned with the value 10, and print the value to the Memo2. Later the same variable is reassigned with value [1,2,3] and print the value. The script used is as follows, test.Value = 10 print(test, test.Value) test.Value = [1,2,3] print(test)Here the variable is created for Module ‘__main__’.
  • Memo1 used for providing the Python Script and Memo2 for showing the output. Buttons to perform the execution, load script from, save the script to a file, show variable content to a message.
  • On Clicking Execute Button the python script is executed using the below code.

Load the custom script to the Memo1 from a file using the code,

Save the Script to the file using the code,Delphi/Pascal

Show the later assigned value after clicking Execute Script using the below code upon clicking Show var content.

Python4Delphi Demo03

Check some of the tutorials available for Python4Delphi here

Related posts
DelphiDelphiVCLPythonPython GUIWindows

Learn about the DelphiVCL.Component as the Common Ancestor of All Component Classes

Learn PythonPythonVideos

Learn Python With The Hidden Power of the Python Runtime by Elizaveta Shashkova (PyCon 2020) Videos

Learn PythonPythonVideos

Learn Python With Organizing to Improve Your Workplace: Know Your Rights by Shauna Gordon-McKeon (PyCon 2020) Video

DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Manipulating Datetime Using Delorean Library In A Delphi Windows App

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.