#!/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.Stat开发者_C百科icText(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()
Without print 'test', it even runs, but with print 'test', it doesn't run nor redirect output.
How to redirect standard output to print messages in gui instead of terminal?
When you use the print
statement with wxPython, where it ends up depends on how you called wx.App()
.
wx.App(redirect=False)
or simply wx.App(0)
will send print statements to a console window, otherwise they will be sent to a little textbox window.
I finally found:
http://www.velocityreviews.com/forums/t515815-wxpython-redirect-the-stdout-to-a-textctrl.html
精彩评论