我在 GooeyPi 应用程序中的 wxPython 中执行此操作。它运行 pyInstaller 命令并在 textctrl 中逐行捕获输出。
在主应用程序框架中,有一个调用OnSubmit的按钮:
def OnSubmit(self, e):
...
# this is just a list of what to run on the command line, something like [python, pyinstaller.py, myscript.py, --someflag, --someother flag]
flags = util.getflags(self.fbb.GetValue())
for line in self.CallInstaller(flags): # generator function that yields a line
self.txtresults.AppendText(line) # which is output to the txtresults widget here
和CallInstaller 执行命令的实际运行,产生一行以及运行 wx.Yield(),因此屏幕不会冻结得太严重。您可以将其移至其自己的线程,但我没有打扰。
def CallInstaller(self, flags):
# simple subprocess.Popen call, outputs both stdout and stderr to pipe
p = subprocess.Popen(flags, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll() # waits for a return code, until we get one..
line = p.stdout.readline() # we get any output
wx.Yield() # we give the GUI a chance to breathe
yield line # and we yield a line
if(retcode is not None): # if we get a retcode, the loop ends, hooray!
yield ("Pyinstaller returned return code: {}".format(retcode))
break