forked from lmbelo/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.pas
More file actions
169 lines (155 loc) · 4.86 KB
/
Copy pathUnit1.pas
File metadata and controls
169 lines (155 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
unit Unit1;
{$I Definition.Inc}
interface
uses
Classes, SysUtils,
{$IFDEF MSWINDOWS}
Windows, Messages, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls,
{$ENDIF}
{$IFDEF LINUX}
QForms, QDialogs, QStdCtrls, QControls, QExtCtrls,
{$ENDIF}
PythonEngine, PythonGUIInputOutput;
type
TForm1 = class(TForm)
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
Splitter2: TSplitter;
Memo1: TMemo;
GroupBox1: TGroupBox;
Button1: TButton;
PythonEngine1: TPythonEngine;
PythonGUIInputOutput1: TPythonGUIInputOutput;
edName: TEdit;
cbInformatician: TCheckBox;
rgSex: TRadioGroup;
cbPythonUser: TCheckBox;
cbTitle: TComboBox;
Label1: TLabel;
Label2: TLabel;
edAge: TEdit;
Label3: TLabel;
PythonModule1: TPythonModule;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
procedure PythonModule1Initialization(Sender: TObject);
private
{ Déclarations privées }
function GetProperty(pSelf, Args : PPyObject) : PPyObject; cdecl;
function SetProperty(pSelf, Args : PPyObject) : PPyObject; cdecl;
function GetPropertyList(pSelf, Args : PPyObject) : PPyObject; cdecl;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
with GetPythonEngine do
begin
// Execute script
ExecStrings( Memo1.Lines );
end;
end;
procedure TForm1.PythonModule1Initialization(Sender: TObject);
begin
with Sender as TPythonModule do
begin
AddDelphiMethod( 'GetProperty', GetProperty, 'GetProperty(PropName) -> PropValue' );
AddDelphiMethod( 'SetProperty', SetProperty, 'SetProperty(PropName, PropValue) -> None' );
AddDelphiMethod( 'GetPropertyList', GetPropertyList, 'GetPropertyList() -> List of property names' );
end;
end;
function TForm1.GetProperty(pSelf, Args : PPyObject) : PPyObject; cdecl;
var
key : PAnsiChar;
begin
with GetPythonEngine do
if PyArg_ParseTuple( args, 's:GetProperty',@key ) <> 0 then
begin
if key = 'Title' then
Result := VariantAsPyObject(cbTitle.Text)
else if key = 'Name' then
Result := VariantAsPyObject(edName.Text)
else if key = 'Informatician' then
Result := VariantAsPyObject(cbInformatician.Checked)
else if key = 'PythonUser' then
Result := VariantAsPyObject(cbPythonUser.Checked)
else if key = 'Age' then
Result := VariantAsPyObject(edAge.Text)
else if key = 'Sex' then
Result := VariantAsPyObject(rgSex.ItemIndex)
else
begin
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Format('Unknown property "%s"', [key])));
Result := nil;
end;
end
else
Result := nil;
end;
function TForm1.SetProperty(pSelf, Args : PPyObject) : PPyObject; cdecl;
var
key : PAnsiChar;
value : PPyObject;
begin
with GetPythonEngine do
if PyArg_ParseTuple( args, 'sO:SetProperty',@key, @value ) <> 0 then
begin
if key = 'Title' then
begin
cbTitle.Text := PyObjectAsVariant( value );
Result := ReturnNone;
end
else if key = 'Name' then
begin
edName.Text := PyObjectAsVariant( value );
Result := ReturnNone;
end
else if key = 'Informatician' then
begin
cbInformatician.Checked := PyObjectAsVariant( value );
Result := ReturnNone;
end
else if key = 'PythonUser' then
begin
cbPythonUser.Checked := PyObjectAsVariant( value );
Result := ReturnNone;
end
else if key = 'Age' then
begin
edAge.Text := PyObjectAsVariant( value );
Result := ReturnNone;
end
else if key = 'Sex' then
begin
rgSex.ItemIndex := PyObjectAsVariant( value );
Result := ReturnNone;
end
else
begin
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Format('Unknown property "%s"', [key])));
Result := nil;
end;
end
else
Result := nil;
end;
function TForm1.GetPropertyList(pSelf, Args : PPyObject) : PPyObject; cdecl;
begin
with GetPythonEngine do
begin
Result := PyList_New(6);
PyList_SetItem(Result, 0, PyString_FromString('Title'));
PyList_SetItem(Result, 1, PyString_FromString('Name'));
PyList_SetItem(Result, 2, PyString_FromString('Informatician'));
PyList_SetItem(Result, 3, PyString_FromString('PythonUser'));
PyList_SetItem(Result, 4, PyString_FromString('Age'));
PyList_SetItem(Result, 5, PyString_FromString('Sex'));
end;
end;
end.