Python for Delphi problem

Mitch Chapman Mitch.Chapman at bioreason.com
Mon Feb 10 21:55:24 CET 2003


Kyle Yancey wrote:
> I've been trying to get the hang of embedding python using the Python
> for Delphi components. I want to be able to write my gui in Delphi and
> leave all the backend work for python.  Unfortunately, I keep getting
> an EAccessViolation from my sample program. I hate to ask, because I
> know this is going to be something simple, but I just can't figure
> this one out.  The program is supposed to import a module and add each
> key to a listbox.
 > [snip]

The explicit initialization of the Python engine isn't needed, since it
was created by dropping a PythonEngine on your form.  The engine should
get initialized automagically when Delphi is loading up your dfm file.

But the real savings come from using the VarPyth unit.  (Note: I'm not
sure what version of Delphi you need in order for this to work.
Delphi 6 definitely works.)  The button-click event handler becomes
almost as legible as if it were written in Python.

------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
     moduleName: string;
     module: variant;
     moduleContents: variant;
     i: integer;
begin
moduleName := Edit1.Text;
module := VarPyth.Import(moduleName);
moduleContents := module.__dict__.keys();
ListBox1.Clear;
for i := 0 to moduleContents.Length() - 1 do
     begin
     ListBox1.Items.Add(moduleContents.getItem(i));
     end;
end;
------------------------------------------------------------------------


Here are the complete Unit1.dfm and Unit1.pas, in case it helps:
------------------------------------------------------------------------
Unit1.dfm:

object Form1: TForm1
   Left = 192
   Top = 107
   Width = 523
   Height = 453
   Caption = 'Form1'
   Color = clBtnFace
   Font.Charset = DEFAULT_CHARSET
   Font.Color = clWindowText
   Font.Height = -11
   Font.Name = 'MS Sans Serif'
   Font.Style = []
   OldCreateOrder = False
   PixelsPerInch = 96
   TextHeight = 13
   object ListBox1: TListBox
     Left = 8
     Top = 8
     Width = 169
     Height = 409
     ItemHeight = 13
     TabOrder = 0
   end
   object Edit1: TEdit
     Left = 192
     Top = 16
     Width = 121
     Height = 21
     TabOrder = 1
     Text = 'Edit1'
   end
   object Button1: TButton
     Left = 328
     Top = 16
     Width = 75
     Height = 25
     Caption = 'Import Module'
     TabOrder = 2
     OnClick = Button1Click
   end
   object PythonEngine1: TPythonEngine
     Left = 192
     Top = 56
   end
end

------------------------------------------------------------------------
Unit1.pas:

unit Unit1;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,
   Dialogs, StdCtrls, PythonEngine, VarPyth;

type
   TForm1 = class(TForm)
     ListBox1: TListBox;
     Edit1: TEdit;
     Button1: TButton;
     PythonEngine1: TPythonEngine;
     procedure Button1Click(Sender: TObject);
   private
     { Private declarations }
   public
     { Public declarations }
   end;

var
   Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
     moduleName: string;
     module: variant;
     moduleContents: variant;
     i: integer;
begin
moduleName := Edit1.Text;
module := VarPyth.Import(moduleName);
moduleContents := module.__dict__.keys();
ListBox1.Clear;
for i := 0 to moduleContents.Length() - 1 do
     begin
     ListBox1.Items.Add(moduleContents.getItem(i));
     end;
end;

end.
------------------------------------------------------------------------

--
Mitch





More information about the Python-list mailing list