补充说明上次的wx.App类,wx.App是应用程序的基类,它会在方法调用wx.Frame类,然后循环调用自己的方法,来运行一个完整的应用程序。如何解释这个事情呢,程序员的方法—你站在这里别动,我打几行代码给你看看,就几行。
wx.App类的运行过程
#coding:utf-8
import wx
import time
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Hello World',size=(300,300))
panel = wx.Panel(self)
txt = wx.StaticText(panel,-1,'Hello world')
self.Centre()
class MyApp(wx.App):
def OnInit(self):
print('App Oninit')
self.frame = MyFrame()
self.frame.Show()
return True
def OnExit(self):
print('App OnExit')
time.sleep(3)
return True
print('App start')
app = MyApp()
print('before Mainloop')
app.MainLoop()
print('after Mainloop')
代码如上,比起上次的代码,这次多了MyApp类(继承了wx.App),用来解释整个过程。从wx.App类的运行开始说起:
print('App start')
app = MyApp()
print('before Mainloop')
跟其他类创建的对象时类似,会先调用其的构造方法,而wx.App程序在创建对象时,则是会调用On

本文详细介绍了wx.App类在WxPython GUI开发中的作用和运行过程。通过创建自定义的MyApp类,揭示了wx.App如何启动、执行OnInit、进入MainLoop以及OnExit的过程。在OnInit中初始化窗口对象,MainLoop则维持程序运行,直到退出条件满足。OnExit用于清理资源。理解这些概念有助于更好地掌握WxPython应用的生命周期。
&spm=1001.2101.3001.5002&articleId=88049628&d=1&t=3&u=fa13ffbf791b4d8295f02eb61faa8c50)
142

被折叠的 条评论
为什么被折叠?



