Noutați

Integrate Python Threading, Windows Console, And Command Line Arguments In Delphi Windows GUI Apps

We know how to do Multithreading in Delphi. How about a simple python script that performs threading in Python and runs the script in the Delphi application? This brings the advantage of using existing multithreaded or new python scripts in your Delphi application. This post guide you to do that with the Python4Delphi Sample app. You can also use Python4Delphi with C++Builder. Python4Delphi Demo22 Sample App shows how to create a Python script in Delphi Application which demonstrates creating thread and output the result in windows console, passing arguments in the command line. You can find the Demo22 source on GitHub. Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi. Components used in Python4Delphi Demo22 App: TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class. You can find the Python4Delphi Demo22 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo22.dproj. Open this project in RAD Studio 10.4.1 and run the application. Implementation Details: PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property. It uses a Python Script file threading_test.py where the below list of modules was used. threading: This module constructs higher-level threading interfaces on top of the lower level _thread module.  collections: This module implements specialized container datatypes providing alternatives to Python’s general-purpose built-in containers, dict, list, set, and tuple. In this sample, deque is used. time: This module provides various time-related functions. threading_test.py contains, ProducerThread, ConsumerThread, BoundedQueue class within the function _test. Memo1, used for providing the Python Script to execute. On Clicking Execute Button the below code executes the python script. procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; procedure TForm1.Button1Click(Sender: TObject); begin   PythonEngine1.ExecStrings( Memo1.Lines ); end; The script below is executed which will opens a window console, create threads put in a queue and execute one after other. import threading_test import sys # the following is needed to use the newly allocated console! sys.stdout = sys.stderr= open(‘CONOUT$’, ‘wt’) try: count = int(sys.argv[1]) except: count = 3 for i in range(count): print (“**** Pass”, i) threading_test._test() print (“**** Done.”) import threading_test   import sys # the following is needed to use the newly allocated console! sys.stdout = sys.stderr= open(‘CONOUT$’, ‘wt’)   try:   count = int(sys.argv[1]) except:   count = 3   for i in range(count):   print (“**** Pass”, i)   threading_test._test() print (“**** Done.”) Head over and check out the open source Python4Delphi project which makes it easy to build Python GUIs for Windows with Delphi.

Read More

Learn About The Initialization Of Class Objects By Rvalues In C++ Windows Development

C++Builder includes the use of rvalue references, which allow creating a reference to temporaries. When you initialize to an class object using an rvalue(a temporary object), C++11 looks to see if you have defined a move constructor in your class. If you have, the temporary object is passed to it as a modifiable (non-const) rvalue reference, allowing you to transfer ownership of resource pointers and handles, and nullify them in the temporary object. We can implement the move constructor as follows.   class SomeClass { private: int *foo; public: SomeClass() : foo(nullptr) {} //Move Constructor SomeClass(SomeClass &&c) { foo = c.foo; c.foo = nullptr; } }; int _tmain(int argc, _TCHAR* argv[]) { SomeClass obj = SomeClass(50); //Move constructor is called. return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class SomeClass {   private:      int *foo;     public:   SomeClass() : foo(nullptr) {}   //Move Constructor   SomeClass(SomeClass &&c)   {      foo = c.foo;      c.foo = nullptr;   } };   int _tmain(int argc, _TCHAR* argv[]) {   SomeClass obj = SomeClass(50); //Move constructor is called.     return 0; } Here use the syntax && to indicate that the variable is an rvalue reference. When the temporary object is initialized, we now simply copy the pointer instead of the content it points to. Head over and check out all of the C++ features supported by the Clang compiler in C++Builder.

Read More

Using Python4Delphi with C++Builder (webinar)

David I. has a fantastic blog post on using Python4Delphi with C++Builder. This was inspired by our previous webinars on the topic. and is the result of his collaboration with Kiriakos (AKA PyScripter), the maintainer of Python4Delphi, who also made some changes in the library to work better with C++Builder. By popular request, David and Kiriakos have also agreed to run a Python for C++ developers webinar where you can learn to leverage Python from your favorite C++ developer tools. Date: Wed, Dec 2nd, 2020 Time: 9 AM CST/1500 UTC  

Read More

Apple Platforms Patch for RAD Studio 10.4.1

We have just released a new patch focused on improving RAD Studio 10.4.1 support for XCode 12, iOS 14 and macOS 11 Big Sur (Intel): these are operating systems and tools which were not available when 10.4.1 shipped. Specifically, the patch offers fixes for a Delphi exception issue on macOS 11 Big Sur Intel (which was also affecting PAServer when running on that platform, meaning this patch includes a new version of PAServer), SDK import from Xcode 12, and debugging applications on an iOS 14 device. Notice that new ARM-based Macs running macOS 11 Big Sur can execute macOS apps built for the Intel platform, including those built with Delphi 10.4.1, via the Apple Rosetta 2 compatibility layer. The patch can be installed via GetIt (with automatic installation via a deferred package, applied as you restart RAD Studio) or a direct download from my.embarcadero.com (available shortly) and manual installation. In both cases you’ll have to copy the PAServer for macOS installer to your Mac and install it manually. The readme file includes additional information and details.

Read More

TMS WEB Core embraces ever more powerful PWA support from Google Chrome

There is no doubt that Google is and remains the driving force behind the fast evolving web world. And with Microsoft having adopted the Google Chromium engine in its latest and new default Windows 10 operating system browser, it is clear that the browser gets ever more powerful with each release. While every Chrome update sees a lot of enhancements, the release 86 introduced perhaps another disruptive feature: the file system access API! Yes, you read this correct, file system access API or in other words, access to the local file system from a PWA (Progressive Web App)! Of course, Google took the necessary measures to look over security. I can imagine you wouldn’t want to be directed to some URL and the web application at this URL will suddenly start scanning your local hard drive. All local file access remains initiated by user interaction and user consent! User consent prompt to view files in a folder What the File System Access API actually provides is: open local text or binary files save to a local text or binary files use the operating system file dialog to pick a file use the operating system file dialog to save to a file get access to a folder and its files/subfolders use the operating system to select a folder associate file types with PWA apps Now, you will understand that as we read the news, we were eager to investigate integration capabilities in a TMS WEB Core PWA and reflect on bringing easy to use Pascal language wrapper classes to take advantage of this new functionality. So, in our labs, we created a support unit for local file handling : WEBLib.LocalFiles. In this unit, three classes are available: TTextFile, TBinaryFile and TFolder. Working with local text files TTextFile offers following public interface TTextFile = class(TObject) public procedure OpenFile; overload; procedure OpenFile(AOpenFile: TOpenTextFileProc); overload; procedure SaveFile; overload; procedure SaveFile(ASaveFile: TSaveFileProc); overload; procedure SaveAsFile; overload; procedure SaveAsFile(ASaveFile: TSaveFileProc); overload; property Text: string; property FileName: string; property OnFileOpen: TNotifyEvent; property OnFileSave: TNotifyEvent; end; As you can see there are overloads for the OpenFile, SaveFile and SaveAsFile methods. In one version of the methods, there is an anonymouse method that is called when the action on the file completed and the other versioni will signal completion via an event. To use the class to open a file, we can write: begin ATextFile := TTextFile.Create; ATextFile.OpenFile(procedure(AText: string) begin WebMemo1.Lines.Text := AText; end); end; If the text was modified in the memo control, it can be saved with following code working on the same instance of the TTextFile object: begin ATextFile.Text := WebMemo1.Lines.Text; ATextFile.SaveFile(procedure begin ShowMessage(‘File succesfully saved’); end); end; As you can see, it becomes extremely easy for Pascal developers to take advantage of the file system access API in the browser! Working with local binary files For binary files, a similar class TBinaryFile was created with the important different from the TTextFile that here the data read or to be saved is of the type TJSArrayBuffer. TJSArrayBuffer is the Object Pascal wrapper for the JavaScript ArrayBuffer which basically is an array of bytes. The interface of TBinaryFile is the same as TTextFile except that it exposes public property TBinaryFile.Data: TJSArrayBuffer. To demonstrate its use, we can open a local image file with the following code: var […]

Read More

Double vector graphics, double quality

uses AdvTypes; procedure TForm1.LoadSVG; begin Image1.Picture.LoadFromFile(‘nature.svg’); end; uses AdvPDFLib, Types; procedure TForm1.Button1Click(Sender: TObject); var p: TAdvPDFLib; begin p := TAdvPDFLib.Create; try p.BeginDocument(‘SVGToPDF.pdf’); p.NewPage; p.Graphics.DrawImageFromFile(‘nature.svg’, RectF(50, 50, p.PageWidth – 50, 500)); p.EndDocument(True); finally p.Free; end; end; When zooming in, you’ll notice that the quality is not affected because of the native vector graphics used inside the PDF document.

Read More

Beautiful Responsive Home Screen UI Templates For FireMonkey Available For Free Via GetIt In The IDE

New FireMonkey developers tend to make unresponsive and bad user experience. For this, we should show guidelines and sample projects to learn how to create stunning FireMonkey projects.  This FireMonkey UI template includes three different designs for implementing an app home screen in a multi-device application.  You can get these sample UI templates from GetIt easily!  What can you learn from these samples? Use cases of TFrame Tab Controls Blur backgrounds Menu and Navigation  Simple & grid layouts and many more Be sure to check out these samples to learn more about FireMonkey app designing guidelines and FireMonkey multi-device responsive application designing! Be sure to check out other resources on building FireMonkey applications:

Read More

Ultimate Compression Toolkit For Delphi And C++ Builder Developers

procedure TAbZipperTests.CreateAndTestBasicZipFile; var   ExtractDir, TestFileName : string;   AbUnZip : TAbUnZipper; begin   // Test with Setting BaseDirectory and not specifying AutoSave   TestFileName := TestTempDir + ‘basic.zip’;   if FileExists(TestFileName) then     DeleteFile(TestFileName);   Component.FileName := TestFileName;   Component.BaseDirectory := TestFileDir;   Component.AddFiles(‘*.*’,faAnyFile);   Component.Save;   Component.FileName := ”;   CheckFileExists(TestFileName);     AbUnZip := TAbUnZipper.Create(nil);   try     AbUnZip.FileName := TestFileName;     // Clean out old Directory and create a new one.     Extractdir := TestTempDir + ‘extracttest’;     if DirectoryExists(ExtractDir) then       DelTree(ExtractDir);     CreateDir(ExtractDir);     // Extract Files.     AbUnZip.BaseDirectory := ExtractDir;     AbUnZip.ExtractFiles(‘*.*’);     // Compare Extracted Files     CheckDirMatch(TestFileDir,ExtractDir);   finally     AbUnZip.Free;   end;   DeleteFile(TestFileName); end;

Read More

Archaeological Museum Utilizes Powerful RAD Server And Beacon Fence Solution With Delphi

Miyazaki Prefectural Saitohara Archaeological Museum has built an app for smartphones, “Saitohara Archaeological Expo Navi,” which provides navigation in the museum, guidance on exhibits, etc. in multiple languages ​​using solutions provided by Embarcadero and Marubeni Information Systems. did. This solution consists of a power-saving beacon that emits radio waves (using “rapiNAVI Air2” manufactured by Marubeni Information Systems) and Embarcadero’s software development function “Beacon Fence” that detects the position using this beacon. At the Saitohara Archaeological Museum, you can use the in-house navigation system “Saitohara Archaeological Expo Navi” that utilizes beacons. With this system, you can check the current location of the museum on your smartphone, and read the explanation of the exhibits in front of you in four languages: Japanese, English, Chinese, and Korean. In this session, we will introduce how this system was built at the museum, along with an overview of the system and the features of RAD Studio / Delphi + BeaconFence used. Case Study http://forms.embarcadero.com/saitobaru-case Website http://saito-muse.pref.miyazaki.jp/web/english/index.html Screenshot Gallery

Read More

Learn To Use Events To Build Python GUI Apps For Windows With Delphi And C++

An event links an occurrence in the system with the code that responds to that occurrence. The occurrence triggers the execution of a procedure called an event handler. The event handler performs the tasks that are required in response to the occurrence. Events allow the behavior of a component to be customized at design-time or at run time. Do you want to trigger and handle an event similar to Delphi events? Python4Delphi provides a mechanism to do that. This post will guide you on how to create events using Python4Delphi Components. Python4Delphi Demo21 Sample App shows how to create a Module, Python type, and add an event to it and define the event, Import the module and Python Type in a python script, and access the created event. You can find the Demo21 source on GitHub. Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi. Components used in Python4Delphi Demo21 App: TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class. TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output. TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can create events using the Events property. After creating the event with a name, its handler can be defined by clicking the OnExecute method which takes the sender, PythonObject, and a variable parameter result as argument. TPythonType: It’s inherited from TGetSetContainer class contains a set of APIs to create, manage a Python Type in Delphi. Similar to TPythonModule, this also has Events property. TPyPoint Delphi class implementing a new Python type. It must derive from TPyObject or one of its descendants. Then it must override some methods, like the constructors, the RegisterMethods, and the type services’ virtual methods. TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line. You can find the Python4Delphi Demo21 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo21.dproj. Open this project in RAD Studio 10.4.1 and run the application. Implementation Details: PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property. PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currentlyexecuting Python script. PythonModule1 with Module name spam is created. Created an eventPythonModule1.Events[0]. And on execute event(PythonModule1Events0Execute) defined with below code. procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf, Args: PPyObject; var Result: PPyObject); begin with GetPythonEngine do begin Result := PyUnicodeFromString(‘Hello world !’); end; end; procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf,   Args: PPyObject; var Result: PPyObject); begin   with GetPythonEngine do   begin     Result := PyUnicodeFromString(‘Hello world !’);   end; end; PythonType1 during initialization, associate PyObjectClass with the created TPyPoint class. Created an event PythonType1.Events[0]. And on execute event(PythonType1Events0Execute) defined with below code. procedure TForm1.PythonType1Events0Execute(Sender: TObject; PSelf, Args: PPyObject; var Result: PPyObject); var dx, dy : Integer; Instance : TPyPoint; begin with GetPythonEngine do begin // Convert the PSelf Python object to a Delphi instance pointer. Instance := TPyPoint(PythonToDelphi(PSelf)); // first we extract the arguments if PyArg_ParseTuple( args, ‘ii:Point.Offset’,@dx, @dy ) 0 then begin // if it’s ok, then we call the method […]

Read More