DelphiLearn PythonPythonPython GUIWindows

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

screen shot 2021 01 10 at 10 30 56 pm 5886221

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!

screen shot 2021 01 10 at 5 22 19 pm 1903775

If you liked learning String Formatting with Python4Delphi, Check out some more cool tutorials on Python4Delphi over here.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

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

CodeIDELearn PythonPythonPython GUITkinter

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

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

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