wxpython入门_wxpython笔记:Wxpython入门
#!/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()
總結
以上是生活随笔為你收集整理的wxpython入门_wxpython笔记:Wxpython入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【BZOJ-3033】太鼓达人
- 下一篇: 深入浅出Mysql 读书笔记