• winfrom桌面程序调用python解释器


      Winfrom桌面程序调用python解释器执行py脚本后台执行完成具体的功能,为什么要这样处理呢?因为我现在的大部分过项目都是后台的脚本处理,界面基本的输入完成之后,将参数按照规则传入到脚本的入口,根据参数的不同执行不同的脚本流程,如果要修改某一个处理过程或者添加新的模块,不需要修改前台的任何代码,只需要在脚本中修改就可以达到需求的效果,简单、方便,风险较小,影响可控等优点。

      因此,我做了一个demo,仅供参考,不足之处,请赐教!

    界面如图:

    Start_exe_py的执行代码如下:

     1  private void Start_exe_Py_Click(object sender, EventArgs e)
     2         {
     3             string txtName = this.textName.Text.Trim();
     4             string CurentPath = System.Windows.Forms.Application.StartupPath;
     5             string pyExecPath = CurentPath + @"..3rdToolsPython27python.exe";
     6             string pyWorkingDir = CurentPath + @"..";
     7             string pyFilePath = CurentPath + @"..UserDefinedScriptsMain.py";
     8             string pyArgv = "";
     9             pyArgv = " " + txtName;
    10 
    11             Cursor.Current = Cursors.WaitCursor;
    12             Process pro = new Process();
    13 
    14             //不显示窗口
    15             pro.StartInfo.RedirectStandardOutput = true;
    16             pro.StartInfo.UseShellExecute = false;
    17             pro.StartInfo.CreateNoWindow = true;
    18             
    19             //显示窗口
    20             //pro.StartInfo.RedirectStandardOutput = false;
    21             //pro.StartInfo.UseShellExecute = true;
    22             //pro.StartInfo.CreateNoWindow = false;
    23 
    24             pro.StartInfo.FileName = pyExecPath;
    25             pro.StartInfo.WorkingDirectory = pyWorkingDir;
    26             pro.StartInfo.Arguments = pyFilePath + pyArgv;
    27             pro.Start();
    28 
    29             pro.WaitForExit();
    30             Cursor.Current = Cursors.Default;
    31         }

    具体的目录结构方式参考:

    用户自定义的脚本代码:

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # @Date    : 2016-06-30 09:10:44
     4 # @Author  : stlong 
     5 # @Version : 1.0
     6 
     7 import os
     8 import sys
     9 
    10 def main():
    11     print "Hello Python!"
    12     print raw_input("python input:")
    13 
    14     
    15 if __name__ == '__main__':
    16     print sys.argv[0]
    17     print sys.argv[1]
    18     main()

    由于时间关系不足之处,请见谅! 

  • 相关阅读:
    java.sql.SQLException: 数字溢出 的解决办法
    oracle数据库创建表,序列及添加代码案例
    Oracle创建用户、角色、授权、建表
    HttpSession与Hibernate中Session的区别
    RuntimeException与CheckedException
    >Hibernate 报错:this project is not a myeclipse hibernate project . assuming hibernate 3 cap
    解决java web项目导入后出现的问题 ---cannot be read or is not a valid ZIP file
    JDK,JRE,JVM区别与联系
    最爱的天籁之音
    applicationContext.xml 基本配置
  • 原文地址:https://www.cnblogs.com/stlong/p/5629242.html
Copyright © 2020-2023  润新知