Learn PythonPython GUI

Learn How To Use Lists With Python GUI In A Delphi Windows App

screen-shot-2021-01-08-at-3-47-53-am-4760003

The strength of Delphi combined with Python gives its users the chance to fast track their development processes and deliver excellent applications with ease! This tutorial helps you accomplish just that. With Python4Delphi (P4D) you can create a GUI for your python scripts in a few simple steps! P4D is a collection of free components that wrap up the Python DLL into Delphi and C++Builder. This post will demonstrate how we can use Python Lists within the Delphi development environment to save our data and display it in the GUI. For this purpose, our approach will be to create a VCL application and run our script inside of it. The output will be visible as soon as the script is executed.

Python4Delphi Demo1 is a Sample App that shows how a Python Script can be run by entering Python code inside of a Memo. The execution and display of results will be conducted inside a separate Memo. The Demo1 Source is available on GitHub at the following link.

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.

In order to run the Python script in Python for Delphi, open and run project Demo1. Then insert the script into lower Memo, click Execute button, and get the result in upper Memo. If you have successfully completed the prerequisites and downloaded Demo1 on your computer, simply open and run the project Demo1. You will see a small window titles “Demo of Python” This window will have an upper and a lower Memo. Enter your Python script in the lower Memo and click execute. You shall see the results on the upper Memo soon after.

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 :

Let’s take a look at the Python code for creating a list. When it comes to Python, this job can be pretty easy. All we have to do is write the name we wish to assign to our list, followed by an ‘=’ operator. Once that is done, we can enter as many values as we like by enclosing them in square brackets as shown below. Currently, our list contains three different names i.e. John, Ella and Harry. We’ll put the VCL application that we just created using Delphi to use by entering this script in the lower Memo. Of course nothing will be displayed until we write the second line of our code which is a print statement. This would initiate display, and in our GUI Memo, we’ll be able to view the complete printed list.

But let’s not stop there. What if we wish to display multiple lines on our upper Memo? That’s not a problem either. The GUI is capable enough of displaying many different lines, all you have to do is add the code.

This time, we’re adding another name to the end of our list i.e. Roland. In Python, we can traverse lists from left to right by using indexes ‘0’ onwards and we can also traverse lists backwards by indexing -1, -2, -3 and so on for as long as the list goes. In this case, the GUI should display only the last element i.e. Roland. Let’s execute the script and see if it works.

screen-shot-2021-01-07-at-9-17-19-pm-6792370
Our VCL at the bottom right, displaying the contents of our two print statements

Check out some more cool tutorials on Python4Delphi over 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

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

pt_BRPortuguese