Python的好處,就是類庫超多,多到隻有你想不到的庫,而沒有你想到的,他卻沒有的庫。是以飛信,在Python下也有一個開源的類庫,這個就是PyFetion,他自己有帶一個Demo,不過是一個CGI的程式,沒有視窗界面,于是用之前Delphi寫的DxVcl為這個飛信庫實作了一個簡單的界面GUI。代碼很簡單,就是兩個視窗,一個視窗是驗證碼輸入的視窗,還有一個是主視窗。界面資訊:
主代碼如下:
<a></a>
class SeriForm(Form):
def __init__(self,Owner):
self.Caption = '請輸入驗證碼'
self.Position = 5
self.BorderStyle = 3
self.Width = 275
self.Height = 162
self.lbl = Label(self)
self.lbl.SetProps(Parent = self,Caption = '請輸入驗證碼')
self.lbl.SetBounds(24,16,72,13)
self.EdtNum = Edit(self)
self.EdtNum.Parent = self
self.EdtNum.SetBounds(102,11,139,21)
self.Img = Image(self)
self.Img.Parent = self
self.Img.SetBounds(24,35,217,59)
self.Img.Center = True
self.Img.Picture.LoadFromFile('fetion_verify.jpg')
self.BtnOk = Button(self)
self.BtnOk.SetProps(Parent = self,Caption='确定')
self.BtnOk.SetBounds(24,100,75,25)
self.BtnOk.OnClick = self.BtnOkClick
self.BtnCancel = Button(self)
self.BtnCancel.SetProps(Parent = self,Caption='取消')
self.BtnCancel.SetBounds(166,100,75,25)
self.BtnCancel.OnClick = self.BtnCancelClick
def BtnCancelClick(self,Sender):
self.Close()
def BtnOkClick(self,Sender):
self.ModalResult = mrok
def GetSeriCode(self,picFile):
"""picFile 驗證碼圖檔"""
SeriFrm = SeriForm(None)
if SeriFrm.ShowModal() == mrok:
ret = SeriFrm.EdtNum.Text
else:
ret = ''
SeriFrm.Free()
return ret
class MainForm(Form):
self.SetProps(Width=492,Height=401,BorderStyle=3)
self.lbUser = Label(self)
self.lbUser.SetProps(Parent = self,Caption = '使用者')
self.lbUser.SetBounds(16,8,24,13)
self.EdtUser = Edit(self)
self.EdtUser.Parent = self
self.EdtUser.SetBounds(55,4,121,21)
self.lbl.SetProps(Parent = self,Caption = '密碼')
self.lbl.SetBounds(192,8,24,13)
self.EdtPwd = Edit(self)
self.EdtPwd.SetProps(Parent = self,PasswordChar='*')
self.EdtPwd.SetBounds(234,4,121,21)
self.lbl1 = Label(self)
self.lbl1.SetProps(Parent = self,Caption = '好友清單')
self.lbl1.SetBounds(8,27,48,13)
self.FriendList = ListBox(self)
self.FriendList.Parent = self
self.FriendList.SetBounds(8,47,137,314)
self.Memo1 = Memo(self)
self.Memo1.Parent = self
self.Memo1.SetBounds(151,47,325,185)
self.Memo2 = Memo(self)
self.Memo2.Parent = self
self.Memo2.SetBounds(151,238,325,87)
self.BtnSend = Button(self)
self.BtnSend.SetProps(Parent = self,Caption = '發送')
self.BtnSend.SetBounds(401,331,75,25)
self.BtnSend.OnClick = self.BtnSendClick
self.BtnLog = Button(self)
self.BtnLog.SetProps(Parent = self,Caption = '登入')
self.BtnLog.SetBounds(361,3,75,25)
self.BtnLog.OnClick = self.BtnLogClick
self.Phone = PyFetion('','','TCP')
self.threads = []
def BtnSendClick(self,Sender):
if self.Phone and self.Phone.alive:
if self.Phone.send_sms(toUTF8(self.Memo2.Lines.Text)):
self.Memo1.Lines.Add('給自己發送短資訊成功,目前隻寫了給自己發送資訊')
ShowMessage('無效的登入')
def BtnLogClick(self,Sender):
if self.BtnLog.Caption == '登出':
self.Phone.logout()
self.BtnLog.Caption = '登入'
self.FriendList.Items.Clear()
return 1
self.Phone.mobile_no = self.EdtUser.Text
self.Phone.passwd = self.EdtPwd.Text
try:
ret = self.Phone.login(FetionOnline)
except PyFetionSupportError,e:
ShowMessage('手機号未開通飛信')
except PyFetionAuthError,e:
ShowMessage('手機号密碼錯誤')
except PyFetionSocketError,e:
ShowMessage(e.msg)
finally:
pass
if ret:
ShowMessage('登入成功')
#增加好友清單
buddys = self.Phone.get_contactlist()
if not buddys:
ShowMessage('無好友')
for i in buddys:
if buddys[i][0]=='':
buddys[i][0]=i[4:4+9]
for i in range(len(buddys)):
s = "%-4d%-8s%-20s" % (i,status[buddys[buddys.keys()[i]][2]].decode('gb2312').encode('utf8'),buddys[buddys.keys()[i]][0],)
s = s.decode('utf8').encode('gb2312')
self.FriendList.Items.Add(s)
self.threads.append(fetion_recv(self)) #啟動接收包
self.threads.append(fetion_alive(self.Phone)) #啟動心跳
for t in self.threads:
t.setDaemon(True)
t.start()
ShowMessage('失敗')
def guimain(argv=None):
PyFetion.GetSeirCodeEvent = GetSeriCode
Application.Initialize()
f = MainForm(Application)
f.Show()
FreeConsole()
Application.Run()
本文轉自 不得閑 部落格園部落格,原文連結:http://www.cnblogs.com/DxSoft/archive/2011/04/08/2009132.html ,如需轉載請自行聯系原作者