Winfrom Desktop Program calls the Python interpreter executes the py script background execution to complete the specific function, why should this be handled? Because I now most of the project is the background of the script processing, the interface basic input is completed, the parameters in accordance with the rules passed to the entrance of the script, according to the different parameters to execute different script process, if you want to modify a process or add a new module, do not need to modify the foreground of any code, Only need to modify in the script can achieve the effect of demand, simple, convenient, less risk, the impact of controllable and other advantages.
Therefore, I have made a demo, only for reference, deficiencies, please enlighten!
Interface
The execution code for START_EXE_PY is as follows:
1 Private voidStart_exe_py_click (Objectsender, EventArgs e)2 {3 stringTxtname = This. TextName.Text.Trim ();4 stringCurentpath =System.Windows.Forms.Application.StartupPath;5 stringPyexecpath = Curentpath +@"\.. \3rdtools\python27\python.exe";6 stringPyworkingdir = Curentpath +@"\.. \";7 stringPyfilepath = Curentpath +@"\.. \userdefinedscripts\main.py";8 stringPYARGV ="";9PYARGV =" "+txtname;Ten OneCursor.current =Cursors.waitcursor; AProcess Pro =NewProcess (); - - //do not show Windows thePro. Startinfo.redirectstandardoutput =true; -Pro. Startinfo.useshellexecute =false; -Pro. Startinfo.createnowindow =true; - + //Display window - //Pro. Startinfo.redirectstandardoutput = false; + //Pro. Startinfo.useshellexecute = true; A //Pro. Startinfo.createnowindow = false; at -Pro. Startinfo.filename =Pyexecpath; -Pro. Startinfo.workingdirectory =Pyworkingdir; -Pro. Startinfo.arguments = Pyfilepath +pyargv; - Pro. Start (); - in Pro. WaitForExit (); -Cursor.current =Cursors.Default; to}
The specific directory structure method reference:
User-definable script code:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #@Date: 2016-06-30 09:10:444 #@Author: Stlong5 #@Version: 1.06 7 ImportOS8 ImportSYS9 Ten defMain (): One Print "Hello python!" A PrintRaw_input ("python input:") - - the if __name__=='__main__': - PrintSys.argv[0] - PrintSys.argv[1] -Main ()
Due to the lack of time relationship, please forgive me!
Winfrom Desktop program calls Python interpreter