Python4Lazarus Aufruf einer Python Funktion

Rund um die LCL und andere Komponenten
Antworten
Acia6850
Beiträge: 58
Registriert: Mo 9. Okt 2023, 18:45
OS, Lazarus, FPC: Windows + WSL / Linux Debian Rasbian OS (L 3.4.0 FPC 3.2.3)
CPU-Target: 64Bit
Wohnort: LK Ludwigsburg

Python4Lazarus Aufruf einer Python Funktion

Beitrag von Acia6850 »

Hallo,

versuche gerade mit Python4Lazarus eine Python-Funktion aufzurufen.
Ich finde leider keine Beispiele.

Wer kann mir ein paar Tipps geben.

Python Code

Code: Alles auswählen

#-------------------------------------------------------------------------------
# Name:        LazPythonFncDemo
# Purpose:
#
# Author:      Acia6850
#
# Created:     23.02.2026
# Copyright:   (c) Acia6850 2026
# Licence:     <your licence>
#-------------------------------------------------------------------------------

val1 = 20
val2 = 5

def PyAdd(a,b):
    c = a+b
    return(c)

def PyStrFnc():
    sVal = 'Hello Python from Lazarus'
    print(sVal)
    print(len(sVal))
    print(sVal.count('l'))

def main():
    print(PyAdd(val1,val2))
    PyStrFnc()

if __name__ == '__main__':
    main()
Lazarus Code

Code: Alles auswählen

program cLazPyFnc;

{$mode objfpc}{$H+}

uses
  {$IFDEF LINUX}
  cthreads,
  {$ENDIF}
  Sysutils, Classes, xdmpython;

Const
  conPySriptPath = '..\..\Python\Scripts\';

procedure xRun;
Var
  slOut : TStringList;

begin
  DmPy := TDmPy.Create(nil);
  try
    WriteLn(DmPy.PyEng.RegVersion);
    slOut := TStringList.Create;
    try
      slOut.Clear;
      slOut.LoadFromFile(conPySriptPath + 'LazPythonFncDemo.py');

      DmPy.PyEng.ExecStrings(slOut);


    finally
      slOut.Free;
    end;

  finally
    DmPy.Free;
  end;
end;

begin
  xRun;
end.      

Code: Alles auswählen


unit xdmpython;

{$mode Delphi}

interface

uses
  Classes, SysUtils, PythonEngine;

type

  { TDmPy }

  TDmPy = class(TDataModule)
    PyEng  : TPythonEngine;

  private

  public

  end;

var
  DmPy: TDmPy;


implementation

{$R *.lfm}

end.       

Benutzeravatar
theo
Beiträge: 11214
Registriert: Mo 11. Sep 2006, 19:01

Re: Python4Lazarus Aufruf einer Python Funktion

Beitrag von theo »

Probier mal so wie in der GUI Anwendung im Anhang.
Das Script liegt in "InitScript".
Nur mal kurz zusammengehackt, damit du einen Schritt weiter kommst.
Es gibt sicher noch viele andere Möglichkeiten.
Dateianhänge
pylaz.zip
(138.05 KiB) 216-mal heruntergeladen

Acia6850
Beiträge: 58
Registriert: Mo 9. Okt 2023, 18:45
OS, Lazarus, FPC: Windows + WSL / Linux Debian Rasbian OS (L 3.4.0 FPC 3.2.3)
CPU-Target: 64Bit
Wohnort: LK Ludwigsburg

Re: Python4Lazarus Aufruf einer Python Funktion

Beitrag von Acia6850 »

Vielen Dank,

jetzt habe ich einen Einstieg.

Grüße
Acia6850

Antworten