DelphiLearn PythonPythonPython GUIRAD Studio

Learn To Build A Flexible Hello World Python GUI In A Delphi Windows App

python hello world in python gui

How about combining the strength of Delphi and Python for your applications to provide first-class solutions for your customer needs? Or you are an expert in Delphi who want to also work with Python, as it is a high and growing demand in the market out there?

Thinking about how to do it? Don’t worry! Python4Delphi does for us. Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and C++Builder. They let you easily execute Python scripts, create new Python modules and new Python types. This post will guide you on how to use these components, create a VCL application, run a simple Python Hello World! script in it, and gets the output.

You can easily build Python GUI apps using your favorite Python language features and libraries for Windows using Delphi and C++Builder and Python4Delphi. In order to run the Python script in Python for Delphi, open and run project Demo01. Then insert the script into lower Memo, click Execute script button, and get the result in upper Memo. You can find the Demo01 source on GitHub.

Python4Delphi Demo01 Sample App shows how to run a Python Script by typing the python code in a Memo, execute and populate the result in another Memo. You can find the Demo01 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.

First, open the Demo01.dproj with RAD Studio:

p-3217553-8122780

 

Components used in Python4Delphi Demo01 App:

1. TPythonEngine

A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class. Some of the key properties for the component listed here.

  • AutoLoad: Enable this property to load the python DLL automatically. It defaults to True.
  • AutoUnload: Enable this property to unload the python DLL automatically. It defaults to True.
  • DllName: By default, it reflects the latest e.g. python39.dll. But you can provide your registered version.
  • DllPath: Physical location of the Python DLL.
  • InitScript: You can provide the python script here to execute implicitly.
  • IO: Associate the TPythonGUIInputOutput component to this property.
  • RegVersion: Registered python version e.g 3.9.
  • UseLastKnownVersion: If you want to use the latest registered Python version installed, set this property to True.

2. 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.

  • MaxLineLength: Maximum character length per line.
  • MaxLines: Maximum python script lines can be executed using this component.
  • OutPut: Associate the Memo component to show the output.
  • RawOutput: Produce the result as raw output.
  • UniCodeIO: Set true to provide Input and output as Unicode.

3. 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 Demo01 sample project from the extracted GitHub repository ../python4delphi/Demos/Demo01/Demo01.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.
  • PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script.
  • Memo1 is used for providing the Python Script and Memo2 for showing the output. Buttons to perform the execution, load script from, and save the script to file.
  • PythonGUIInputOutput1‘s property Output to Memo2.
  • On Clicking Execute script Button the script strings are executed using the below code:
  • Load the script to the Memo1 from a file using the below code:
  • Save the script to the file using the below code:
  • And then, click “Run Without Debugging (Shift+Ctrl+F9)“:
2_runwithoutdebugging
Run Without Debugging (Shift+Ctrl+F9).
  • Type and run “Hello World!” and Python Basic Mathematical Operations to see it in actions:
3 pythonhelloworld
Python Hello World and Mathematical Operations with Python4Delphi

Congratulations, now you can successfully run Hello World Python GUI In A Delphi Windows App! Which as you may know that even “Hello World!” is often the first program written by people learning to code, it can also be used as a sanity test to make sure that a computer language is correctly installed and that the operator understands how to use it.

From now, the applications are endless and the only limit is your imagination.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And OpenCV Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Radial Basis Function Networks (RBFNs)

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *