Learn PythonPythonPython GUIWindows

Learn How To Use Multiple Python Function Arguments In A Delphi Windows GUI App

screen shot 2021 01 15 at 5 45 26 pm

Python, the infamous general purpose object oriented language known to be a business and data-science favorite has now become close friends with another powerful tool! We’re talking about Delphi, the tool that allows developers to build stunning visual applications. Project Python4Delphi, more commonly known as (P4D) is a go! 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.

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.

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. In order to run the Python script in P4D, open and run project Demo1. Then insert a Python script into lower Memo, click the Execute button, and you will be able to view the result in upper Memo, if you have successfully completed the prerequisites and downloaded Demo1 on your computer. You will see a small window titled “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.

If you are interested to know more about Demo1, 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 :

When dealing with Functions in Python, it is highly likely we wish to send multiple arguments to our functions. Having that said, there are more than one methods for accomplishing this task. Let’s say that you know the exact number of arguments you need to pass and you are confident that this requirement is not going to change over time. You can use the fixed argument method as shown below.

In the code above, we knew exactly how many arguments we wanted to pass through the function. In case you wish to add more arguments you can simply add one inside the function definition, right next to arg1 and arg2.

However, sometimes we do not know how many arguments we will need to pass and that is where things get tricky. Well, not really. It’s pretty straight forward! Whenever you need to enter multiple arguments in your function, without knowing the exact number, the ” * ” operator can be used. Refer to the code below for a quick demo.

In the function above, we used (*args) thus letting letting Python know, that it can expect any number of arguments from our code. The list of arguments was passed down can be accessed by iteration using for loops. As an example, we tried out passing 4 different arguments but you can feel free to add more when you try it out for yourself!

Let’s take a look at another method that can be used to pass down arguments to a function. This time, with keywords included!

Now that we’ve covered enough grounds with code, let’s try this all out!

screen shot 2021 01 14 at 5 58 27 pm 2074652

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 NumPy Library?

CodeIDEProjectsPythonWindows

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

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?

Leave a Reply

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