PierreC 1 Posted December 13, 2021 Hello everyone, I have another question: is there a way to get the prompt argument of the python input() function? For example if the python code is input("Please, give me a number") I would like to get this string when dealing with the OnReceiveData or OnReceiveUniData events. Share this post Link to post
pyscripter 689 Posted December 14, 2021 (edited) You cant't, at least not easily. The input function writes to stdout the message and then reads from stdin the input. The two operations are separate. You may be able to guess from what was written to sys.stdout last. But what you can do is overwrite builtins.input with your own function. Edited December 14, 2021 by pyscripter Share this post Link to post
PierreC 1 Posted December 15, 2021 Ok, thank you. I will have a look at this possibility. Share this post Link to post
iqrf 3 Posted May 17, 2023 On 12/14/2021 at 3:49 PM, pyscripter said: But what you can do is overwrite builtins.input with your own function. How to override builtins.input please? Share this post Link to post
pyscripter 689 Posted May 17, 2023 (edited) Run a script like this: import builtins def myinput(prompt): builtins._input_prompt = prompt builtins._input(prompt) builtins._input = builtins.input builtins.input = myinput Then the prompt will be stored in builtins._input_prompt Edited May 17, 2023 by pyscripter Share this post Link to post
iqrf 3 Posted May 18, 2023 Thanks a lot, the text is passed, but the input value is always NoneType. import builtins def myinput(prompt): builtins._input_prompt = prompt builtins._input(prompt) builtins._input = builtins.input builtins.input = myinput nadr = int(input('Enter device address (0 - 239, 255): ')) print(f'Node {nadr} is not bonded') procedure TForm1.PythonInputOutput1ReceiveUniData(Sender: TObject; var Data: string); var s: string; begin InputQuery( 'Query from Python', MainModule.builtins._input_prompt, s); Data := AnsiString(s); end; Enter device address (0 - 239, 255): Traceback (most recent call last): File "<string>", line 11, in <module> TypeError : int() argument must be a string, a bytes-like object or a real number, not 'NoneType' Share this post Link to post
iqrf 3 Posted May 18, 2023 I will answer myself, this is functional import builtins def myinput(prompt): builtins._input_prompt = prompt return builtins._input(prompt) builtins._input = builtins.input builtins.input = myinput Thanks again, I'm learning Python and I'm still amazed at what it can do. 1 Share this post Link to post
pyscripter 689 Posted May 18, 2023 (edited) I wrote the code without testing, just to give you the idea. Good that you solved the problem. Also instead of MainModule.builtins._input_prompt you can use BuiltinModule._input_prompt. And the following would do. No need to convert to AnsiString and back. procedure TForm1.PythonInputOutput1ReceiveUniData(Sender: TObject; var Data: string); begin InputQuery( 'Query from Python', BuiltinModule._input_prompt, Data); end; Edited May 18, 2023 by pyscripter 1 Share this post Link to post