DelphiLearn PythonPythonPython GUIWindows

Learn How To Use String Formatting In A Python GUI App With Delphi


Python is one of the world’s most popular languages and Delhpi is an amazing platform that allows developers to build stunning visual apps. Together, Python and Delphi result in the extraordinary feature that we call Python4Delphi. P4D is a set of free components which work by wrapping up the Python DLL into Delphi and C++Builder thus enabling its users to view GUI for their Python scripts with ease. In this tutorial, we will show you just how easy it is to set up your P4D project and start building!

In order to view GUI for our Python scripts, we need to start by making a VCL app. Luckily for us, this can easily be achieved by running the Demo1 project which will be made available to you in the following gitHub repository link. We will now proceed to explaining how you can run Demo1. Let’s take a look at the prerequisites.

Prerequisites: Before we begin working, it is essential we Download the latest version of Python for your platform. You can find the installation instructions for Python4Delphi at this link. Alternatively, you can follow the easy instructions found in this video Getting started with Python4Delphi.

The first step towards getting our VCL app up and running is by opening and running project Demo1. Upon successful completion of Step 1, you will be able to see a new window titled “Demo of Python” This window will have two different Memos, the upper and lower. The latter is for writing your python scrips. It can be anything from a simple “Hello World” to String Formatting, in our case. All GUI elements will be printed on the upper Memo, as shown in the screenshot below. Once you have written down your python script, all you need to do is click on the button “Execute Script” and viola! The GUI will work.

The implementation details of how Delphi manages to run your Python code behind the scenes and which code is executed in order to accomplish that can be found at this link.

The Python Code:

Python supports two different styles of String Formatting. The first style is similar to the C language and has been around since the dawn of time (Python’s lifetime). However, a newer, simpler style was introduces in Python3 which is has been widely adopted. In this tutorial, we will show both styles of formatting and let you pick your favorite!

What you see above is the C Style or the old method of formatting. It allows its users a lot of different options. “%s” is used to format any String into your main string. It can also be used for values with a possible string representation. “%d” is used for integers such as 1,23, etc. “%f” for floating point numbers and %.<number of decimal places>f for floats with a fixed number of decimal places. %X can be used to represent the HEX version of any number whereas %x shows the same, in lower case. Let’s take another Example!

So by now, we are well versed with some examples of the old system. Let’s take a look at the newer version that Python has to offer!

So how would this code reflect itself in the GUI? We can find that out by simply running the scripts written above in Python4Delphi’s Demo1 VCL Application. Screenshot attached below!


If you liked learning String Formatting with Python4Delphi, Check out some more cool tutorials on Python4Delphi over 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

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *