Skip to content Skip to sidebar Skip to footer

Redirect Command Prompt Output To A Python Generated Window

Developed a script which builds a project using msbuild. I have GUI developed using wxpython which has a button on which when user clicks would build a project using msbuild. Now,

Solution 1:

I actually wrote about this a few years ago on my blog where I created a script to redirect ping and traceroute to my wxPython app: http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/

Basically you create a simple class to redirect stdout to and pass it an instance of a TextCtrl. It ends up looking something like this:

classRedirectText:def__init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

    defwrite(self,string):
        self.out.WriteText(string)

Then when I wrote my ping command, I did this:

defpingIP(self, ip):
    proc = subprocess.Popen("ping %s" % ip, shell=True, 
                            stdout=subprocess.PIPE) 
    printwhileTrue:
        line = proc.stdout.readline()                        
        wx.Yield()
        if line.strip() == "":
            passelse:
            print line.strip()
        ifnot line: break
    proc.wait()

The main thing to look at is the stdout parameter in your subprocess call and the wx.Yield() is important too. The Yield allows the text to get "printed" (i.e. redirected) to stdout. Without it, the text won't show up until the command is finished. I hope that all made sense.

Solution 2:

I made a change like below,it did work for me.

defbuild(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) 
    printwhileTrue:
        line = proc.stdout.readline()                        
        wx.Yield()
        if line.strip() == "":
            passelse:
            print line.strip()
        ifnot line: break
    proc.wait() 

Post a Comment for "Redirect Command Prompt Output To A Python Generated Window"