将命令提示符输出重定向到python生成的wind

2024-09-27 17:40:04 发布

您现在位置:Python中文网/ 问答频道 /正文

开发了一个使用msbuild生成项目的脚本。我用wxpython开发了GUI,wxpython有一个按钮,当用户单击该按钮时,将使用msbuild生成一个项目。现在,我想在用户单击该按钮时打开一个状态窗口,显示命令提示符和命令提示符中显示的所有输出,即将命令提示符输出重定向到用户GUI状态窗口。我的构建脚本是

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

Tags: 项目用户self脚本return状态wxpythongui
2条回答

我做了一个如下的改变,它确实对我有用。在

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() 

实际上,几年前我在我的博客上写过这篇文章,我在博客中创建了一个脚本,将ping和traceroute重定向到我的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命令时,我这样做了:

^{pr2}$

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

相关问题 更多 >

    热门问题