Learn PythonPython GUI

Learn How To Use Python Dictionaries In A Delphi Windows GUI App

screen shot 2021 01 13 at 1 15 42 pm

Building stunning visual applications has never been easier, and now with Python4Delphi, it’s about to get even better! Delphi provides a powerful set of tools that enable developers around the globe to build incredible visual applications using C++Builder and Python is one of the world’s most popular object oriented general purpose languages. Python4Delphi now enables you to 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 an application that was put forward by Delphi to show 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.

To get started, make sure the Demo1 project file has been downloaded from the GitHub repository linked above. Next, open and run the Project Demo1. Upon successful compilation of the code, you will observe a new window titled “Demo of Python”. This window will have two separate segments or Memos. The top Memo is for displaying the GUI whereas the bottom segment is for entering the Python script. Once you have successfully entered the script, click execute. You shall see the results on the top 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 :

Without further delay, let’s dive right into the code for using Dictionaries in Python. Dictionaries are often used in Python to store values. They work by saving values in Key Value Pairs. It’s also important to remember that Dictionaries do not allow duplicates. So a user can never save two values against the same key. Dictionaries are also used in many algorithm implementations as a means for counting frequency for different items. This Python collection is unordered and changeable.

In the following code, we’re simply creating a dictionary using curly brackets. In this example, the data stored is by a school to maintain a record of their students. Although different database management systems and object oriented programming offer much better ways of handling information, we adopt this example to explain dictionaries. As you can observe in the code below that the same dictionary is able to store and handle different datatypes.

The first print statement simply calls for the entire dictionary to be printed. Whereas the second print statement requires only the value which was stored against they key “class” this key is of string datatype. However, it should be known that both keys and values and belong to any data type. Once the code gets executed, the value against class, i.e. “Second Grade” will get printed out as a result of the second print statement. Observe:

screen shot 2021 01 12 at 7 58 20 pm 3232405

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?

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

Leave a Reply

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