Python 重定向标准输出以在gui而不是终端中打印消息

Python 重定向标准输出以在gui而不是终端中打印消息,python,Python,如果没有打印“test”,它甚至会运行,但如果使用打印“test”,它既不会运行也不会重定向输出 如何将标准输出重定向到gui而不是终端中打印消息?当您将print语句与wxPython一起使用时,它在哪里结束取决于调用wx.App()的方式 wx.App(redirect=False)或干脆wx.App(0)将把打印语句发送到控制台窗口,否则它们将被发送到一个小文本框窗口。当使用wxPython的print语句时,它的最终位置取决于调用wx.App()的方式 wx.App(redirect=F

如果没有打印“test”,它甚至会运行,但如果使用打印“test”,它既不会运行也不会重定向输出


如何将标准输出重定向到gui而不是终端中打印消息?

当您将
print
语句与wxPython一起使用时,它在哪里结束取决于调用
wx.App()
的方式


wx.App(redirect=False)
或干脆
wx.App(0)
将把打印语句发送到控制台窗口,否则它们将被发送到一个小文本框窗口。

当使用wxPython的
print
语句时,它的最终位置取决于调用
wx.App()
的方式

wx.App(redirect=False)
或干脆
wx.App(0)
将打印语句发送到控制台窗口,否则将发送到一个小文本框窗口。

我最终发现:

我终于发现:


请修复setstd()的缩进。我很确定我知道你的意思,但是缩进在Python中有点重要。请修复setstd()的缩进。我很确定我知道你的意思,但是缩进在Python中有点重要。
#!/usr/bin/python

import wx
import os
import sys

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(480, 400))
        self.panel = MyPanel(self, -1)
        self.Centre()
        self.Show(True)
        setstd()
        print 'test'
        """
        syncall()
        """

class MyPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.text = wx.StaticText(self, -1, '', (40, 60))

class MyText():
    def write(string):
        label = frame.panel.text.GetLabel()
        frame.panel.text.SetLabel(string)

def setstd():
    sys.stdout = MyText()
    sys.stderr = MyText()

app = wx.App()
frame = MyFrame(None, -1, 'DropBox log')
app.MainLoop()