将命令提示符输出重定向到python生成的窗口
开发了一个使用msbuild构建项目的脚本。我使用wxpython开发了GUI,它有一个按钮,当用户单击该按钮时,将使用msbuild构建项目。现在,我想在用户单击该按钮时打开一个状态窗口,显示命令提示符中显示的所有输出,并且不应显示命令提示符,即,将命令提示符输出重定向到用户GUI状态窗口。我的构建脚本是将命令提示符输出重定向到python生成的窗口,python,python-2.7,wxpython,Python,Python 2.7,Wxpython,开发了一个使用msbuild构建项目的脚本。我使用wxpython开发了GUI,它有一个按钮,当用户单击该按钮时,将使用msbuild构建项目。现在,我想在用户单击该按钮时打开一个状态窗口,显示命令提示符中显示的所有输出,并且不应显示命令提示符,即,将命令提示符输出重定向到用户GUI状态窗口。我的构建脚本是 def build(self,projpath) arg1 = '/t:Rebuild' arg2 = '/p:Configuration=Release' arg3
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
事实上,我几年前在我的博客上写到了这一点,我在博客中创建了一个脚本,将ping和traceroute重定向到我的wxPython应用程序: 基本上,您可以创建一个简单的类来重定向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()
主要要看的是子流程调用中的stdout参数,wx.Yield也很重要。产量允许打印文本,即重定向到标准输出。没有它,文本在命令完成之前不会显示。我希望这一切都是有意义的。几年前,我在我的博客上写到了这一点,在那里我创建了一个脚本,将ping和traceroute重定向到我的wxPython应用程序: 基本上,您可以创建一个简单的类来重定向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()
主要要看的是子流程调用中的stdout参数,wx.Yield也很重要。产量允许打印文本,即重定向到标准输出。没有它,文本在命令完成之前不会显示。我希望这一切都有意义。我做了如下改变,它确实对我有用
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()
我做了如下的改变,它确实对我有用
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()