开发者

Simplest way to integrate python gui app with c console app

I have a c console app which converts a c file to a html file, the c file location is passed to the program as a command line argument.(the app is for the windows platform)

What I would like to do is have a python gui app to allow the user to select a file and pass the location of the file to the c app for processing.

I already know how to create a basic python gui with tkinter, but I can't figure out or find any usefull info on how I would go about intgrating t开发者_如何学编程he two programs.

Maybe its possible to pipe the string to the c app with the pOpen() method? (but I can't figure out how...)

Note: Im new to python so code examples might be helpful rather then just description, (as Im not familiar with all of the python libraries etc,) although any help at all would be appreciated.


You probably want the subprocess module.

At the very minimum:

import subprocess
retcode = subprocess.call(["/path/to/myCprogram", "/path/to/file.c"])
if retcode == 0:
   print "success!"

This will run the program with the arguments, and then return its return code.

Note that subprocess.call will block until the program has completed, so if it's not one which runs quickly, your entire Tkinter GUI will stop redrawing until it's completed.

For more advanced use, you may want to use subprocess.Popen. This will require you polling until the command has completed, but allows you to do it with less blocking.

If your C program prints the HTML to standard out, you will need to pipe the output like so:

proc = subprocess.Popen(["/path/to/myCprogram", "/path/to/file.c"], 
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err_output = proc.communicate()
# output will now contain the stdout of the program in a string


There are a number of ways you could acheive this, but in your case the requirements seems so straightforward all you need is to use Popen from the subprocess module.

 process = subprocess.Popen(['myutil', file_location]...)
 process.wait()

If you need to get the results back to the caller, use a PIPE for the stdout and read the resulting data from it when the subprocess has finished.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜