Delphi Tutorial: Python For Delphi VCL vs Pyforms GUI

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Tutorial: Python For Delphi VCL vs Pyforms GUI
February 18, 2021 By Muhammad Azizul Hakim

Are you an expert in desktop apps and GUI development who want to also work with Python because of its simplicity, flexible use, and growing demand in the market out there? Or are you a Python Developer at any level who wants to start a GUI development journey? This article is for you.

We will review the two examples of the most powerful Python Desktop App and GUI Frameworks: Python For Delphi (P4D) VCL vs Pyforms GUI.

1. Python For Delphi VCL​

How about combining the strength of GUI and desktop app development by Delphi with Python for your applications, to provide first-class solutions for your customer needs? Python4Delphi is the answer to your need!

Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and Lazarus (FPC). They let you easily execute Python scripts, and powerfully create new Python modules and new Python types.

The best advantage of P4D is, it makes it very easy to use Python as a scripting language for Delphi applications that comes with a very extensive range of customizable demos and tutorials that ready to use to develop real-world apps. In shorts: P4D gives you various real-world “Hello World!” apps that ready to adjust to your needs!

Prerequisites:
Before you begin to work with Python4Delphi VCL, download and install the latest Для просмотра ссылки Войди или Зарегистрируйся for your platform. Follow the Для просмотра ссылки Войди или Зарегистрируйся installation instructions mentioned Для просмотра ссылки Войди или Зарегистрируйся. Alternatively, you can check out the easy instructions found in this video Для просмотра ссылки Войди или Зарегистрируйся and the behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this Для просмотра ссылки Войди или Зарегистрируйся.

Here are some working example of it, runs on RAD Studio:
1613681024981.png
Demo01 to Run any Python Scripts on Delphi.

Let’s try this interesting Demo, Demo31: Using WrapDelphi to access Delphi Form attributes. This demo shows you various attributes in the Delphi VCL Form:
1613681039032.png
Expect User Input, and Add Them to Lists.

1613681051692.png
Expect User to Click Button, and Go to the Next Page.

1613681065412.png
Go Back to the First Page, if the Button Clicked

1613681077354.png
This New Window will Show if You Close the First Page.

1613681088438.png
This List and Check Box Demo will Show Up if You Close the Previous Window.

1613681098574.png
The Document Browser Will Show Up after You Closed the Previous Window.
 

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009

2. Pyforms GUI​

Pyforms GUI is part of the Pyforms framework. Pyforms GUI is a Python 3 framework to allow pyforms applications to execute in Windows GUI mode.

This framework aims to boost the development productivity by providing an API in Python to allow the execution of applications developed for GUI and Web mode in terminal mode.

Pyforms GUI developed by Ricardo Ribeiro from Champalimaud Scientific Software Platform.

How can we use Pyforms GUI?​

The steps for using the GUI package Pyforms are:

  1. Install Pyforms using pip or easy install (note: it will very much easier to install in Anaconda Distributions, because Pyforms GUI will require OpenCV, and other Data Science/Machine Learning packages that have higher compatibility with Anaconda Distributions):
Python:
pip install pyforms-gui
See the installation progress in your command prompt:
1613681162961.png
2. Create your first app.

Create the file demo.py and add the following code:
Python:
from pyforms.basewidget import BaseWidget
from pyforms.controls   import ControlFile
from pyforms.controls   import ControlText
from pyforms.controls   import ControlSlider
from pyforms.controls   import ControlPlayer
from pyforms.controls   import ControlButton
 
class ComputerVisionAlgorithm(BaseWidget):
 
    def __init__(self, *args, **kwargs):
        super().__init__('Computer vision algorithm example')
 
        #Definition of the forms fields
        self._videofile     = ControlFile('Video')
        self._outputfile    = ControlText('Results output file')
        self._threshold     = ControlSlider('Threshold', default=114, minimum=0, maximum=255)
        self._blobsize      = ControlSlider('Minimum blob size', default=110, minimum=100, maximum=2000)
        self._player        = ControlPlayer('Player')
        self._runbutton     = ControlButton('Run')
 
        #Define the function that will be called when a file is selected
        self._videofile.changed_event     = self.__videoFileSelectionEvent
        #Define the event that will be called when the run button is processed
        self._runbutton.value       = self.__runEvent
        #Define the event called before showing the image in the player
        self._player.process_frame_event    = self.__process_frame
 
        #Define the organization of the Form Controls
        self._formset = [
            ('_videofile', '_outputfile'),
            '_threshold',
            ('_blobsize', '_runbutton'),
            '_player'
        ]
 
 
    def __videoFileSelectionEvent(self):
        """
        When the videofile is selected instanciate the video in the player
        """
        self._player.value = self._videofile.value
 
    def __process_frame(self, frame):
        """
        Do some processing to the frame and return the result frame
        """
        return frame
 
    def __runEvent(self):
        """
        After setting the best parameters run the full algorithm
        """
        pass
 
 
if __name__ == '__main__':
 
    from pyforms import start_app
    start_app(ComputerVisionAlgorithm)
Execute the demo.py file in the command prompt, using this command:
Python:
python.exe demo.py
Let’s see the interesting results:
1613681209142.png
This GUI can even run a video inside it!