#!/usr/bin/env python
'''静态文本、可控文本、对话框、GetApp()'''
importwx,time
ID_EXIT=200ID_ABOUT=201
class Frame(wx.Frame): #2 wx.Frame子类
def __init__(self,parent = None,id = -1,title ='wxPython!'):
wx.Frame.__init__(self,parent,id,title,size=(500,500))
self.setupStatusBar()
self.InitButton()
self.InitMenu()#设置状态栏
defsetupStatusBar(self):#状态栏
sb = self.CreateStatusBar(2) #2代表将状态栏分为两个
self.SetStatusWidths([-1, -2]) #比例为1:2
self.SetStatusText("Ready", 0) #0代表第一个栏,Ready为内容
#timmer
self.timer =wx.PyTimer(self.Notify)
self.timer.Start(1000, wx.TIMER_CONTINUOUS)
self.Notify()#处理事件
defOnclick(self,event):if event.GetEventObject()==self._submit_btn:
dlg=LoginDiglog(None,-1)
dlg.ShowModal()
dlg.Destroy()#实时显示时间
defNotify(self):
t=time.localtime(time.time())
st=time.strftime('%Y-%m-%d %H:%M:%S',t)
self.SetStatusText(st,1)#这里的1代表将时间放入状态栏的第二部分上
defInitButton(self):#显示按钮功能
self.panel=wx.Panel(self,-1)
wx.StaticText(self.panel,label="Username",pos=(20,20))
wx.StaticText(self.panel, label="Password", pos=(20, 50))
self._username=wx.TextCtrl(self.panel,pos=(85,15))
self._passwd= wx.TextCtrl(self.panel, pos=(85, 45),style=wx.TE_PASSWORD)
self._submit_btn=wx.Button(self.panel,label=u'提交',pos=(20,80),size=(50,30))
self.panel.Bind(wx.EVT_BUTTON,self.Onclick,self._submit_btn)defGetUsername(self):returnself._username.GetValue()defGetPasswd(self):returnself._passwd.GetValue()defInitMenu(self):#主菜单
menubar =wx.MenuBar()#子菜单:退出(Quit)
fmennu =wx.Menu()
fmennu.Append(ID_EXIT, u'退出(&Q)', 'Terminate thr program')
menubar.Append(fmennu, u'文件(&F)') #将子菜单添加到文件(File)中
#子菜单:关于(About)
hmenu =wx.Menu()
hmenu.Append(ID_ABOUT, u'关于(&A)', 'More information about this program')
menubar.Append(hmenu, u'帮助(&H)') #将子菜单添加到帮助(Help)中
self.SetMenuBar(menubar)#绑定子菜单
wx.EVT_MENU(self, ID_EXIT, self.OnMenuExit)
wx.EVT_MENU(self, ID_ABOUT, self.OnMenuAbout)
wx.EVT_CLOSE(self, self.OnCloseWindow)defOnMenuExit(self,event):
self.Close()defOnMenuAbout(self,event):
dlg=AboutDialog(None,-1)
dlg.ShowModal()
dlg.Destroy()defOnCloseWindow(self,event):
self.Destroy()classLoginDiglog(wx.Dialog):def __init__(self,parent,id):#super(LoginDiglog,self).__init__(parent,id,u"显示",size=(200,200))
wx.Dialog.__init__(self, parent, id, '显示', size=(200, 200))
self.app=wx.GetApp()
self.panel=self.app.frame
self._username_dlg=wx.StaticText(self,label=u'用户名:'+self.GetUsername(),pos=(20,20))
self._passwd_dlg=wx.StaticText(self,label=u'密码:'+self.GetPasswd(),pos=(20,50))
wx.Button(self,wx.ID_OK,pos=(20,80))defGetUsername(self):returnself.panel.GetUsername()defGetPasswd(self):returnself.panel.GetPasswd()classAboutDialog(wx.Dialog):def __init__(self,parent,id):
wx.Dialog.__init__(self,parent,id,'About Me',size=(200,200))#BoxSizer为一个盒子,wx.VERTICAL
self.sizer1=wx.BoxSizer(wx.VERTICAL)
self.sizer1.Add(wx.StaticText(self,-1,u'wxPython初级教程'),0,wx.ALIGN_CENTER_HORIZONTAL|wx.TOP,border=20)
self.sizer1.Add(wx.StaticText(self,-1, u'wxPython初级教程'), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, border=10)
self.sizer1.Add(wx.StaticText(self,-1, u'wxPython初级教程'), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, border=10)
self.sizer1.Add(wx.StaticText(self,-1, u'wxPython初级教程'), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, border=10)
self.sizer1.Add(wx.Button(self,wx.ID_OK),0,wx.ALIGN_CENTER|wx.BOTTOM,border=20)
self.SetSizer(self.sizer1)class App(wx.App): #5 wx.App子类
def __init__(self):#如果要重写__init__,必须调用wx.App的__init__,否则OnInit方法不会被调用
wx.App.__init__(self)defOnInit(self):
self.frame=Frame()
self.SetTopWindow(self.frame)
self.frame.Show()returnTrueif __name__=="__main__":
app=App()
app.MainLoop()