py4d

 

Demo02

Page history last edited by Jan Kurianski 3 yrs ago

Evaluate a Python expression (Kylix ready)

 

WARNING !!! The evaluation of an expression works only for arithmetic expressions

and not for instructions ! The use of variables and functions is of course allowed

but constructs like for, def, class, print, import... are not allowed.

Use Exec... instead (like ExecStrings).

 

  1. Create a new Form
  2. Drop a TMemo (or a TRichEdit)
  3. Drop a TPythonGUIInputOutput for displaying Python's messages
  4. Drop a TMemo for the source code
  5. Drop a TPythonEngine
  6. Connect the attribute IO of the TPythonEngine to the TPythonGUIInputOutput.
  7. Connect the attribute Output of TPythonGUIInputOutput to the TRichEdit.
  8. Drop a TButton and call it "Evaluate script"
  9. Double-click on the button and add:

var

Result : PPyObject;

begin

with PythonEngine1 do

begin

Result := EvalStrings( Memo1.Lines );

if Assigned(Result) then

begin

ShowMessage(Format('Eval: %s',PyObjectAsString(Result)));

Py_DECREF(Result);

end

else

ShowMessage('Could not evaluate the script');

// Or you could simply use:

// ShowMessage('Eval: ' + EvalStringsAsStr( Memo1.Lines ) );

end;

That's all !

  1. Compile and execute.
  2. Write in the Memo1: print 2+2
  3. Click on the Execute button
  4. You should see in the Output window: 4

Comments (0)

You don't have permission to comment on this page.