softtouch 9 Posted October 28 I have a python engine on form 1, dropped to the form, which is working fine. Now I create a module in code in another unit: module:=TPythonModule.create(nil); module.Engine:=GetPythonEngine; module.ModuleName:='delphicallback'; module.OnInitialization:=InitPythonModule; module.Initialize; The InitPythonModule event contains just: TPythonModule(Sender).ClearMethods; TPythonModule(Sender).AddDelphiMethod('callback',delphi_callback,'callback(name, value)->value'); This works, I execute a python script which send data back to the module "delphicallback". All is fine so far, I get the data from the python script. When I close the form, I free the module with module.free; Still all is fine. But when I then try to open the form again, I get the error "No python engine was created" when it creates the module. What did I miss? Share this post Link to post
pyscripter 694 Posted October 28 You should not free modules that have been initialized and loaded into python while the python engine is active. python has references to procedures of the module and this may lead to crashing python. You can destroy modules after python has finailized. Share this post Link to post
softtouch 9 Posted October 28 Thank you, good to know. But I dont create the module in the main unit, where the python component is on the form. I create it instead when another form is created, in the formcreate event and thought I must free it also in the formclose event. So how shall hat work when I dont free the module? It would get always created again when the form is created. Share this post Link to post
pyscripter 694 Posted October 29 (edited) I have no idea what you are trying to accomplish, but for instance you can put the PythonModule in a data module. Instead of using AddDelphiMethod directly. you could add an Event to the PythonModules Event's collection. Events are converted to python functions, which delegate the execution to a Delphi event handler. Then, every time the form is created, it could assign a handler to the event's OnExecute property. Edited October 29 by pyscripter Share this post Link to post