Calling python script and function

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Calling python script and function

Post by Priya »

Hi,

I have developed a application using wxWidgets. I want to call a python script and functions directly from wxWidgets. Is it possible. If so please explain me how to do.

Regards,
Priya
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Calling python script and function

Post by ONEEYEMAN »

Hi,
You probably looking at wxExecute( "python <you_script_name_goes_here" );
Just make sure you cwd is pointing to the right direction.

BTW, out of curiosity - why not use wxPython?

Thank you.
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Re: Calling python script and function

Post by Priya »

Thanks ONEEYEMAN,

I have used wxExecute in both sync and async mode as a separate process. It works fine. But the output (say some 10 printf in one process) redirection to control prints chunk by chunk. Not prints line by line (the 10 printf of one process).

But my aim is print output each line immediate redirected to the wxwidgets control.
so i tried to embed python inside wxwidgets using pthread. it works fine. For windows 32 bit but for 64 bit it was not working.
I also required to call any function of a python script from wxwidgets just like a function call and get the output.

How i can achieve this.

I started working on c++so i used wxwidgets.

can i embed wxpython in wxwidgets or how easily i can do this?

Please respond

regards,
Priya
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Calling python script and function

Post by ONEEYEMAN »

Hi,
for 64 bit it was not working.
What does this mean?
Compilation problem? Crash? Assert? No output/result?

Thank you.
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Re: Calling python script and function

Post by Priya »

Hi,
Compilation Problem, In win 64 bit pc i was not able to use pthread, for win 32 bit pc i downloaded pthread 32 bit dll and lib and supporting include files and placed in visual studio location. For 64 bit i was not able to download pthread.

Is it right way to use pthread approch?? or any other way is there to call python script and function directly from wxWidgets.

Thanks
Priya
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Re: Calling python script and function

Post by Priya »

Hi

Can you provide me some sample how to call python & its function from wxWidgets.

Regards,
Priya
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Calling python script and function

Post by ONEEYEMAN »

Hi,
Priya wrote: Hi,
Compilation Problem, In win 64 bit pc i was not able to use pthread, for win 32 bit pc i downloaded pthread 32 bit dll and lib and supporting include files and placed in visual studio location. For 64 bit i was not able to download pthread.
What do you need a pthread for? Just use standard Windows thread mechanism thru the wxThread....
Using pthread means you have extra dependency in you code.

I would fiorget about that and just use wxWidgets' wxThread.

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Calling python script and function

Post by evstevemd »

Priya wrote:Hi

Can you provide me some sample how to call python & its function from wxWidgets.

Regards,
Priya
You have to use Python C-API or Boost.Python.
Form your posts it seems to be simple task so Python API should suffice. In this regard,

slightly modified from py website

Code: Select all

void main(int argc, char *argv[])
{
  Py_SetProgramName("Awesome Pyprogram");  /* optional but recommended */
  Py_Initialize();
  wxString fileContents;
  // .... read python file using wxFile or similar way into fileContents
  PyRun_SimpleString(fileContents.c_str().AsChar());
  Py_Finalize();
}
if you need interactions then you need to dig deep into the Py C-Api
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply