使用DxVcl为Python的飞信库写一个简单的GUI

简介:

    Python的好处,就是类库超多,多到只有你想不到的库,而没有你想到的,他却没有的库。所以飞信,在Python下也有一个开源的类库,这个就是PyFetion,他自己有带一个Demo,不过是一个CGI的程序,没有窗口界面,于是用之前Delphi写的DxVcl为这个飞信库实现了一个简单的界面GUI。代码很简单,就是两个窗口,一个窗口是验证码输入的窗口,还有一个是主窗口。界面信息:

2011040810500331.png

主代码如下:

复制代码
 
 
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):
def __init__ (self,Owner):
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
= Label(self)
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(
' 给自己发送短信息成功,目前只写了给自己发送信息 ' )
else :
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(
' 手机号未开通飞信 ' )
return 1
except PyFetionAuthError,e:
ShowMessage(
' 手机号密码错误 ' )
return 1
except PyFetionSocketError,e:
ShowMessage(e.msg)
return 1
finally :
pass
if ret:
ShowMessage(
' 登录成功 ' )
# 增加好友列表
buddys = self.Phone.get_contactlist()
if not buddys:
ShowMessage(
' 无好友 ' )
else :
self.BtnLog.Caption
= ' 登录 '
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()
else :
ShowMessage(
' 失败 ' )
return 1


def guimain(argv = None):
PyFetion.GetSeirCodeEvent
= GetSeriCode
Application.Initialize()
f
= MainForm(Application)
f.Show()
FreeConsole()
Application.Run()
复制代码

本代码就是在原作者的Fetion.py上修改来的,仅仅就是套上了一个界面GUI而已,另外发送短信,也就只写了发送给自己而已。如果个人有需要的自行扩展一下吧,嘿嘿,完整代码下载



本文转自 不得闲 博客园博客,原文链接:http://www.cnblogs.com/DxSoft/archive/2011/04/08/2009132.html   ,如需转载请自行联系原作者

相关文章
|
1天前
|
JSON Shell 数据格式
第十章 Python常用标准库使用(必会)
第十章 Python常用标准库使用(必会)
|
2天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
1天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
2天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
8 3
Python
19 0
|
2天前
|
JSON 数据格式 索引
python 又一个点运算符操作的字典库:Munch
python 又一个点运算符操作的字典库:Munch
19 0
|
3天前
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,提供DataFrame数据结构。基本步骤包括导入库、创建DataFrame及进行数据筛选。示例代码展示了如何通过布尔索引、`query()`和`loc[]`方法筛选`Age`大于19的记录。
10 0
|
4天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名
【4月更文挑战第22天】Pandas Python库提供数据排序和排名功能。使用`sort_values()`按列进行升序或降序排序,如`df.sort_values(by='A', ascending=False)`。`rank()`函数用于计算排名,如`df['A'].rank(ascending=False)`。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`和分别对'A'、'B'列排名。
14 2
|
4天前
|
算法 Python
请解释Python中的关联规则挖掘以及如何使用Sklearn库实现它。
使用Python的mlxtend库,可以通过Apriori算法进行关联规则挖掘。首先导入TransactionEncoder和apriori等模块,然后准备数据集(如购买行为列表)。对数据集编码并转换后,应用Apriori算法找到频繁项集(设置最小支持度)。最后,生成关联规则并计算置信度(设定最小置信度阈值)。通过调整这些参数可以优化结果。
25 9
|
5天前
|
Python
如何使用Python的Pandas库进行数据缺失值处理?
Pandas在Python中提供多种处理缺失值的方法:1) 使用`isnull()`检查;2) `dropna()`删除含缺失值的行或列;3) `fillna()`用常数、前后值填充;4) `interpolate()`进行插值填充。根据需求选择合适的方法处理数据缺失。
39 9