2013-03-18 43 views
1

开发了一个使用msbuild构建项目的脚本。我有使用wxpython开发的图形用户界面,它有一个按钮,当用户点击时可以使用msbuild建立一个项目。现在,当用户单击该按钮时,我想打开一个状态窗口,并显示在命令提示符下显示的所有输出,并且不应显示命令提示符,即将命令提示符输出重定向到用户GUI状态窗口。我的构建脚本是,重定向命令提示符输出到python生成的窗口

def build(self,projpath) 
    arg1 = '/t:Rebuild' 
    arg2 = '/p:Configuration=Release' 
    arg3 = '/p:Platform=x86' 
    p = subprocess.call([self.msbuild,projpath,arg1,arg2,arg3]) 
    if p==1: 
     return False 
    return True 

回答

4

其实我写了一篇关于这个几年前在我的博客,我创建了一个脚本来ping和跟踪重定向到我的wxPython应用程序:http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/

基本上你创建一个简单的类将stdout重定向到并传递给TextCtrl的一个实例。它结束了看起来像这样:

class RedirectText: 
    def __init__(self,aWxTextCtrl): 
     self.out=aWxTextCtrl 

    def write(self,string): 
     self.out.WriteText(string) 

后来,当我写我的ping命令,我这样做:

def pingIP(self, ip): 
    proc = subprocess.Popen("ping %s" % ip, shell=True, 
          stdout=subprocess.PIPE) 
    print 
    while True: 
     line = proc.stdout.readline()       
     wx.Yield() 
     if line.strip() == "": 
      pass 
     else: 
      print line.strip() 
     if not line: break 
    proc.wait() 

看最主要的是你的子进程调用和标准输出参数wx.Yield()也很重要。 Yield允许文本“打印”(即重定向)到标准输出。没有它,文本将不会显示,直到命令完成。我希望这一切都有道理。

+0

Thank [email protected]为我工作。 – Aramanethota 2013-03-22 04:49:36

1

我做了如下改变,它确实对我有用。

def build(self,projpath): 
    arg1 = '/t:Rebuild' 
    arg2 = '/p:Configuration=Release' 
    arg3 = '/p:Platform=Win32' 
    proc = subprocess.Popen(([self.msbuild,projpath,arg1,arg2,arg3]), shell=True, 
        stdout=subprocess.PIPE) 
    print 
    while True: 
     line = proc.stdout.readline()       
     wx.Yield() 
     if line.strip() == "": 
      pass 
     else: 
      print line.strip() 
     if not line: break 
    proc.wait() 
相关问题