2013-03-18 57 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() 
相關問題