wxPython:输出重定向

通过一个实例来看看如何控制wxPython的输出,并观察各必要对象的创建次序及生命周期。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
    Function:简单的wxPython程序
    Input:NONE
    Output: NONE
    author: socrates
    blog:http://www.cnblogs.com/dyx1024/
    date:2012-07-01
'''       

import sys    
import wx


class MyFrame(wx.Frame):
    '''
    创建一个Frame类
    '''
    def __init__(self, parent, id, title):
        print u"Frame对象初始化(Frame _init__)"
        wx.Frame.__init__(self, parent, id, title)
              
class MyApp(wx.App):
    '''
    创建一个App类
    '''
    def __init__(self, redirect = True, filename = None):
        print "APP __init__"
        wx.App.__init__(self, redirect, filename)
        
    def OnInit(self):
        print u"APP对象的OnInit方法(OnInit)"
        self.frame = MyFrame(parent = None, id = -1, title = u"测试wxPyhont输出重定向")
        self.frame.Show()
        self.SetTopWindow(self.frame)
        print >> sys.stderr, u"输出到标准错误控制台。"
        return True
    
    def OnExit(self):
        print u"APP对象的OnExit方法"
        
    
def main():
    app = MyApp(redirect = True) #开始重定向
    print u"begin MainLoop"
    app.MainLoop()    
    print u"after MainLoop"
    
if __name__ == '__main__':
    main()    
        

运行一下看看:

 可见,在APP对象创建之后(__init__方法调用结束),由于指定了重定向功能,所以输出切换到了stdout/stderr。

 由上面的输出也可以看到各对象的生命同周期,创建先后顺序:

  1.   app._init_()
  2.   app.OnInit()
  3.   Frame._init()
  4.   app.MainLoop()
  5.   app.OnExit()

观察这一句:app = MyApp(redirect = True) #开始重定向:

  •   当redirect为True时,输出到wxPython框架,此时也可启用filename来指定输出到文件。
  •   当redirect为False时,输出到控制台。

 

  

  

转载于:https://www.cnblogs.com/dyx1024/archive/2012/07/01/2571624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值