pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
906 stars 310 forks source link

Query from Python #379

Closed dominiced closed 2 years ago

dominiced commented 2 years ago

This is presented when Python fires the input() function QueryFromPython

Is there any way to present the input prompt message, rather than just this generic input dialog box with no information present? I would like to include the input argument within the dialog box.

Alexey-T commented 2 years ago
#support input() in plugins
def _input(s):
    return dlg_input(s, '') or ''

import builtins
builtins.input = _input

use script like this.

dominiced commented 2 years ago
#support input() in plugins
def _input(s):
    return dlg_input(s, '') or ''

import builtins
builtins.input = _input

use script like this.

Apologies Alexey-T but I get a NameError with the code provided because 'dlg_input' is not define, where is that meant to come from? Is it meant to be included as part of P4D?

Alexey-T commented 2 years ago

No, dlg_input is CudaText (app with Python4Lazarus) python function, you must write Delphi function and then export it via Py module.

Alexey-T commented 2 years ago
function api_dlg_input(Self, Args : PPyObject): PPyObject; cdecl;
var
  P1, P2: PChar;
  StrCaption, StrVal: string;
begin
  with AppPython.Engine do
    if Bool(PyArg_ParseTuple(Args, 'ss:dlg_input', @P1, @P2)) then
    begin
      StrCaption:= string(P1);
      StrVal:= string(P2);
      if InputQuery(msgTitle, StrCaption, StrVal) then
        Result:= PyUnicodeFromString(StrVal)
      else
        Result:= ReturnNone;
    end
  else
    Result:= ReturnNone;
end;

And

procedure TfmMain.PythonModuleInitialization(Sender: TObject);
begin
  with Sender as TPythonModule do
  begin
    AddMethod('app_ver', @api_app_ver, '');
    AddMethod('app_path', @api_app_path, '');
    AddMethod('app_proc', @api_app_proc, '');
    AddMethod('app_log', @api_app_log, '');
    AddMethod('app_idle', @api_app_idle, '');
    AddMethod('emmet', @api_emmet, '');

    AddMethod('msg_status', @api_msg_status, '');
    AddMethod('msg_status_alt', @api_msg_status_alt, '');
    AddMethod('msg_box', @api_msg_box, '');
    AddMethod('msg_box_ex', @api_msg_box_ex, '');
    AddMethod('dlg_input', @api_dlg_input, '');
dominiced commented 2 years ago

Excellent, thank you Alexey