Python for Delphi problem

Kyle Yancey kyle at lakeofburningfire.org
Mon Feb 10 13:22:02 EST 2003


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.

unit1.dfm
object Form1: TForm1
  Left = 298
  Top = 165
  Width = 535
  Height = 417
  Caption = 'Form1'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ListBox1: TListBox
    Left = 36
    Top = 40
    Width = 141
    Height = 313
    ItemHeight = 13
    TabOrder = 0
  end
  object Edit1: TEdit
    Left = 228
    Top = 86
    Width = 173
    Height = 21
    TabOrder = 1
  end
  object Button1: TButton
    Left = 251
    Top = 176
    Width = 86
    Height = 28
    Caption = 'Import Module'
    TabOrder = 2
    OnClick = Button1Click
  end
  object PythonEngine1: TPythonEngine
    Left = 330
    Top = 17
  end
end


unit1.pas
unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  PythonEngine1.Py_Initialize;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  pyName, pyModule, pyDict, pyList, pyItem : PPyObject;
  Index, pyListSize : Integer;
  Item, TempItem : PChar;
begin
  {import moddule}
  pyName := PythonEngine1.PyString_FromString( PChar(Edit1.Text) );
  pyModule := PythonEngine1.PyImport_Import(pyName);

  if pyModule <> nil then
  begin
    {Get the modules dictionary}
    pyDict := PythonEngine1.PyModule_GetDict(pyModule);
    {Get the keys list from the dictionary}
    pyList := PythonEngine1.PyDict_Keys(pyDict);
    {Get the size of the list}
    pyListSize := PythonEngine1.PyList_Size(pyList);

    {Allocate space for a string to hold the results}
    Item := StrAlloc(21);

    for Index := 0 to pyListSize do
    begin
      {Get each item in the list}
      pyItem := PythonEngine1.PyList_GetItem(pyList, Index);
      {Convert the item to a null terminated string}
      TempItem := PythonEngine1.PyString_AsString(pyItem);
      {Copy to the new Item string}
      StrLCopy(Item, TempItem, 20);
      PythonEngine1.Py_DECREF(pyItem);
      pyItem := nil;
      ListBox1.Items.Add(String(Item));
      PythonEngine1.Py_DECREF(pyList);
    end;
    PythonEngine1.Py_DECREF(pyModule);
  end;
    PythonEngine1.Py_DECREF(pyName);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  PythonEngine1.Py_Finalize;
end;

end.




More information about the Python-list mailing list