Sometimes your application needs a user interface, but what is the best way to make one for Python applications? Enter DelphiVCL for Python. The VCL is a mature Windows native GUI framework with a huge library of included visual components and a robust collection of 3rd party components. It is the premier framework for native Windows applications, but how to use it with Python? Thanks to the DelphiVCL Python package (based on the open-source Python4Delphi library), the VCL is a first-class package for building native Windows GUIs with Python. Need more design tools? You can build the entire GUI in Delphi and then write all the logic in Python.
Table of Contents
Why Native GUI is Important:
- Performance: Windows provides hardware acceleration for native controls.
- Windows Handles: Native Windows controls have Windows handles providing better OS and intra-application integration.
- Consistent Behavior: The use of native controls gives users consistent behavior between all applications they use.
- Microsoft Active Accessibility (MSAA) is the framework that leverages native controls to provide accessibility interfaces like screen readers
Popular Python GUI Frameworks
Tkinter (TK Interface) – The default Python GUI Available under the Python license, uses Tcl/Tk cross-platform widgets. Tk is implemented in C and some Tcl. Supports various styles but No OS style integration. Tkinter sample,
import tkinter as tki
def __init__(self, master=None):
self.list = tki.Listbox(self)
self.editb = tki.Button(self, text="Add", command=self.add_to_list)
self.edit = tki.Entry(self)
self.label = tki.Label(self, text="Hello Python", anchor='w')
self.master = master
self.place(anchor='nw', x=10, y=10, relwidth=1, relheight=1, bordermode='outside')
self.label.place(x=10, y=10, width=100, height=20)
self.edit.place(x=10, y=30, width=250, height=24)
self.editb.place(x=270, y=24, width=100, height=30)
self.list.place(x=10, y=60, width=300, height=300)
root = tki.Tk()
ttl = root.title("A Tkinter form")
app = Application(master=root)
PyQt (pie-cute) – Binding for the cross-platform Qt widget library. Originally by Trolltech (1991) and Nokia (2008). Qt is a set of C++ libraries and development tools that includes Cross-platform abstractions for GUI, Networking, threads, regular expressions, SVG, OpenGL,3D animation, etc. PyQT implements over 1000 of these classes as a set of Python Modules.
- GPL/LGPL license, or Qt Commercial license
- Must be installed, pip install PyQt5
- Fusion style option comes close to matching platform look, but still doesn’t use native controls. PyQT Sample.
from PyQt5.QtWidgets import *
self.setGeometry(10, 10, 280, 280)
self.setWindowTitle('A Qt Form...')
label = QLabel('Hello Python', self)
edit = QLineEdit("", self)
button = QPushButton("Add", self)
listView = QListWidget(self)
app = QApplication(sys.argv)
ex = PyQtLayout()
if __name__ == '__main__':
Other Python GUI Frameworks.
- PySide – Python Qt bindings project, providing access to complete Qt4.8 framework as well as generator tools for rapidly generating bindings for any C++ libraries.
- wxPython – Cross-Platform GUI Python library. It is implemented as a set of Python extension modules that wrap GUI components of the wxWidgets cross-platform library written in C++
- PyGTK – Binding for GTK (GNOME). GTK is a widget toolkit.
- Kivy – Focused on mobile and cross-platform.
Delphi & VCL
- Object Pascal(Delphi) is an extremely powerful language based upon core foundations such as a good program structure and extensible data types. These foundations are partially derived from the traditional Pascal language, but even the core language features have seen many extensions from the early days. Download a copy of the Object Pascal Handbook by Marco Cantu…FREE! .
- VCL Windows-specific VCL (Visual Component Library) is a set of components and classes for the rapid development of Windows applications in the Delphi and C++ languages.
- The GUI components of the VCL are native Windows GUI components.
Python4Delphi & Embarcadero Delphi
- Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi. Easily execute Python scripts, create new Python modules and new Python types.
- Can create Python extensions as DLLs and provides different levels of functionality:
- Low-level access to the python API
- High-level bi-directional interaction with Python
- Access to Python objects using Delphi custom variants (VarPyth.pas)
- Wrapping of Delphi objects for use in python scripts using RTTI (WrapDelphi.pas)
- Creating python extension modules with Delphi classes and functions
- Design Beautiful Desktop and Mobile App UIs with Embarcadero Delphi
- Code Faster and Smarter with Delphi inbuilt and Rich 3rd party Ecosystem.
- Debug Faster with Integrated Native Debugging
- Compile and Deploy High-Performance Native Apps
- Easily Improve the Quality of Your Code, Collaborate and Extend the IDE
DelphiVCL For Python
- DelphiVCL for Python is nothing but the wrapping of Delphi objects for use in python scripts using RTTI (WrapDelphi.pas) explicitly for VCL Components(WrapDelphiVCL.pas)
- Wrappers help to create and access Delphi Objects quickly from python.
- These wrappers contain container classes to extend and expose your custom events, methods, variables which can be used in python script.
How to Build DelphiVCL for Python
- Check the video for how to extend Delphi Objects as a Python Extension module Combining the strengths of Delphi and Python – Python4Delphi replay part 2 from 46.03 minutes.
- Alternatively, we can check the blog post, How to extend PythonModule as a DLL using Python4Delphi. In a similar fashion, a simple Python Module DelphiVCL is created which wraps all the Delphi VCL objects.
Note: For Python developers, it’s less important how to build this DelphiVCL Python Extension Module, rather can learn on available VCL objects and their properties to quickly develop a GUI application. Similar to the Tkinter and PyQt sample app mentioned above, the DelphiVCL sample app is below.
from DelphiVCL import *
def __init__(self, Owner):
self.Caption = "A VCL Form..."
self.SetBounds(10, 10, 340, 410)
self.lblHello = Label(self)
self.lblHello.SetProps(Parent=self, Caption="Hello Python", Top=10, Left=10, Width=100, Height=24)
self.edit1 = Edit(self)
self.edit1.SetProps(Parent=self, Top=30, Left=10, Width=200, Height=24)
self.button1 = Button(self)
self.button1.SetProps(Parent=self, Caption="Add", OnClick=self.Button1Click)
self.lb1 = ListBox(self)
def Button1Click(self, Sender):
Application.Title = "MyDelphiVCLApp"
f = MainForm(Application)
Comparing the Forms of different Frameworks.
OS integration’s :
Apart from these Delphi VCL uses Uses Windows controls and each control has a handle that supports UI testing. None provide native Windows controls or Windows platform integration.
How to run a DelphiVCL sample.
- Download DelphiVCL.7z and extract according to the python version. Where the DelphiVCL.pyd and the sample TestVCL.py found in the extracted folder.
- From the command prompt, navigate to the folder mentioned in step 1. Provide the Pythopn.exe path space DelphiVCL python script. Press the Enter key. e.g) C:DelphiVCLDelphiVCL.64bit-39>C:PythonPython39python.Exe TestVCL.py
- For more VCL components demonstration Download the Delphi4Python.zip folder and extract it. Run the samples Delphi4PythonSample.py and Delphi4PythonComponentsSample.py similar to step 2. Note. This Delphi4Python.pyd is built with Python39.dll
VCL Visual Designers
- The Form Designer (or Designer) is displayed automatically when you are creating or editing a graphical application that uses a form file, either a .dfm or an .fmx file.
- A VCL form displays the standard Windows buttons for Minimize, Resize, and Close commands. Drag and Drop components from ToolPallete and you can view and set properties and events visually using ObjectInspector.
- Ultimate Design Guidelines Support with VCL Components.
- Taking a Snapshot of Your Form (VCL Only)
- LiveBindings is a data-binding feature supported by both the VCL and FireMonkey frameworks. It is an expression-based framework, which means it uses bindings expressions to bind objects to other objects or to dataset fields.
- Create new LiveBindings between various visual components you have on your form.
- Edit the existing LiveBindings.
- Visualize all the LiveBindings that you have created.
- Export your binding diagram as an image file.
With the help of VCL Visual Designers, you can easily develop GUI quickly which later can be wrapped using Python4Delphi and extended as Python Module similar to DelphiVCL. Download the DelphiVCLWebinar.zip folder where you can learn how to wrap custom VCL forms using RAD studio. Note. This is built with Python39.dll and using Rad Studio 10.4.1
Delphi VCL DockWiki’s
YouTube Embarcadero channel link:
- https://www.youtube.com/watch?v=aCz5h96ObUM (Webinar 1)
- https://www.youtube.com/watch?v=ssIKb3nJw5c ( Webinar 2)
- https://www.youtube.com/watch?v=hjY6lBgrHhM ( Getting started with Python4Delphi)
- Learning Delphi and Building Native GUI apps is Easier and faster.
- Quite a lot of resources with a Rich third-party ecosystem are available to get started.
- Python has vast packages to ease developers from basic programming tasks to advanced Scientific, ML, AI, Deep learning-related tasks.
- Combining the Strengths of Delphi and Python helps users to build Ultra-fast Native GUI Apps.