我想要做的只是输出一些终端命令到wx.TextCtrl小部件。我认为最简单的方法就是创建一个自定义的stdout类,并将写入函数重载到该部件的写入函数。创建一个自定义的sys.stdout类?
标准输出类:
class StdOut(sys.stdout):
def __init__(self,txtctrl):
sys.stdout.__init__(self)
self.txtctrl = txtctrl
def write(self,string):
self.txtctrl.write(string)
然后,我会做一些如:
sys.stdout = StdOut(createdTxtCtrl)
subprocess.Popen('echo "Hello World!"',stdout=sys.stdout,shell=True)
什么结果是以下错误:
Traceback (most recent call last):
File "mainwindow.py", line 12, in <module>
from systemconsole import SystemConsole
File "systemconsole.py", line 4, in <module>
class StdOut(sys.stdout):
TypeError: Error when calling the metaclass bases
file() argument 2 must be string, not tuple
任何想法解决这个问题会不胜感激。
我尝试这样做,这导致: 回溯(最近通话最后一个): '! “Hello World” 的回声' 文件 “blade_gui.py”,线路179,在OnNew subprocess.Popen(,标准输出= sys.stdout,shell = True) 文件“C:\ Python26 \ lib \ subprocess.py”,行614,在__init__中 errread,errwrite)= self._get_handles(stdin,stdout,stderr) 文件“C: \'Python26 \ lib \ subprocess.py“,第734行,在_get_handles c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) AttributeError:'StdOut'对象没有'fileno'属性 – Duck 2010-02-19 17:22:38
'subprocess'可以执行写入的程序任何语言(不只是Python!),所以需要一个“真正的操作系统级文件”(即。,一个带有FD,也称为fileno) - 除了一个真正的OS级文件(包括幸运的是,子进程)外,没有办法将任何东西作为subprocess.Popen的'stdout ='传递。PIPE',它可以让你的父进程接收子进程的输出 - 但是,在子进程中的缓冲通常意味着这不是你想要的,所以我总是建议使用'pexpect'或'wexpect'来代替)。 – 2010-02-19 19:03:06
你需要的不仅仅是'write',你还需要'flush',也许一些其他的取决于它的使用方式。 – SimplyKnownAsG 2014-06-03 06:02:39