wxpython 1-应用App(PyApp)

一. class App(PyApp)

你应该重写OnInit来做应用程序初始化以确保系统、工具包和wxWidgets被完全初始化。

1.__init__()

建房的图纸是人绘画的,绘画的过程就是__init__()的过程,我们称之为实例化过程

1、参数:

redirect:当系统stdout或系统stderr的重定向,如果filename=None,输出就会重定向到弹窗
filename:加系统输出重定向到文件中
useBestVisual:尽量使用最好的系统提供的可用视觉效果,必须在__init__()实例化是就设置
clearSigInt:SIGINT应该被清除吗?这允许应用程序在控制台中按Ctrl-C终止GUI应用程序将

2、__init__()是App()类的构造函数
3、构建一个应用程序的对象
4、源代码解析

    def __init__(self, redirect=False, filename=None, useBestVisual=False, clearSigInt=True):
        PyApp.__init__(self)
        # 确保可以创建GUI,IsDisplayAvailable()如果应用程序能够连接到系统的,则返回True
        if not self.IsDisplayAvailable():
            if wx.Port == "__WXMAC__":
                msg = "This program needs access to the screen. Please run with a\n Framework build of python, and only when you are logged in\n on the main display of your Mac."
            elif wx.Port == "__WXGTK__":
                msg ="Unable to access the X Display, is $DISPLAY set properly?"
            else:
                msg = "Unable to create GUI"
                # TODO: more description is needed for wxMSW...
        
            raise SystemExit(msg)
        
        # 允许程序员指定应用程序是否将使用最好的系统可视化,支持多个相同的可视化显示
        self.SetUseBestVisual(useBestVisual)
        
#设置SIGINT的默认处理程序。这解决了一个问题,如果在启动此操作的控制台中按下Ctrl-C,
#app然后它将不做任何事情,(甚至不发送KeyboardInterrupt??),但稍后退出时会出现段错误
#通过设置默认处理程序,然后应用程序将退出,如预期(取决于平台)
        if clearSigInt: # 清除信号
            try:
                import signal
                signal.signal(signal.SIGINT, signal.SIG_DFL)
            except:
                pass
        # 保存并将stdio(输入)重定向到窗口
        self.stdioWin = None
        self.saveStdio = (_sys.stdout, _sys.stderr)
        if redirect:
            self.RedirectStdio(filename)
        
        # 使用Python的安装前缀作为默认值(设置编码等)
        prefix = _sys.prefix
        if isinstance(prefix, (bytes, bytearray)):
            prefix = prefix.decode(_sys.getfilesystemencoding())
        wx.StandardPaths.Get().SetInstallPrefix(prefix)
        wx.SystemOptions.SetOption("mac.listctrl.always_use_generic", 1)
	    #这完成了wxWindows的初始化,然后调用
	    #如果继承这个类时必须有self._BootstrapApp()
        self._BootstrapApp() # 引导程序app

2.OnPreInit()

1、在引导程序完成后必须完成的事情,但是如果在OnInit(初始化)之前已经完成那就直接调用。这可以在派生类中重写,但一定要调用
2、代码解析

    def OnPreInit(self):
        wx.StockGDI._initStockObjects()
        self.InitLocale()

3.SetTopWindow()

设置“main”顶层窗口,它将用于的父窗口按需输出窗口以及不具有的对话框一个显式的父集。

    def SetTopWindow(self, frame):
        if self.stdioWin:
            self.stdioWin.SetParent(frame) # 将给定节点设置为父节点
        wx.PyApp.SetTopWindow(self, frame) # 设置为顶级窗口

4.MainLoop()

执行主GUI事件循环

    def MainLoop(self):
        rv = wx.PyApp.MainLoop(self) # 由wxWidgets在创建应用程序时调用
        self.RestoreStdio() # 重定向系统。stdout和系统。Stderr到一个文件或弹出窗口
        return rv

5.RedirectStdio()

重定向系统stdout和系统Stderr到一个文件或弹出窗口

    def RedirectStdio(self, filename=None):
        if filename:
            _sys.stdout = _sys.stderr = open(filename, 'a')
        else:
            self.stdioWin = self.outputWindowClass()
            _sys.stdout = _sys.stderr = self.stdioWin

6.RestoreStdio()

修复系统stdout和系统Stderr功能

    def RestoreStdio(self):
        try:
            _sys.stdout, _sys.stderr = self.saveStdio
        except:
            pass

7.SetOutputWindowAttributes()

1、设置标题、位置、输出窗口的大小,如果stdio已经重定向。这应该在任何输出之前调用,重新创建新的输出窗口。

2、参数:

title:窗口标题
pos:窗口的定位
size:窗口的大小

    def SetOutputWindowAttributes(self, title=None, pos=None, size=None):
        if self.stdioWin:
            if title is not None:
                self.stdioWin.title = title
            if pos is not None:
                self.stdioWin.pos = pos
            if size is not None:
                self.stdioWin.size = size

8.InitLocale()

尽量确保C和Python区域设置与wxWidgets同步语言环境在Windows。如果你有麻烦从这个默认行为方法,您可以在派生类中重写该方法以改变其行为。请报告您遇到的问题

    def InitLocale(self):
        self.ResetLocale() # 恢复设置
        if 'wxMSW' in PlatformInfo:
            import locale
            try:
                lang, enc = locale.getdefaultlocale() # 获取本地语言
                self._initial_locale = wx.Locale(lang, lang[:2], lang)
                locale.setlocale(locale.LC_ALL, lang)  # 设置语言
            except (ValueError, locale.Error) as ex:
                target = wx.LogStderr()
                orig = wx.Log.SetActiveTarget(target)
                wx.LogError("Unable to set default locale: '{}'".format(ex))
                wx.Log.SetActiveTarget(orig)

9.ResetLocale()

恢复设置

    def ResetLocale(self):
        self._initial_locale = None

10.Get()

返回当前活动的应用程序对象的静态方法,本质就是GetApp()

    @staticmethod
    def Get():
        return GetApp()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值