设为首页 收藏本站
查看: 2946|回复: 0

[经验分享] 使用DxVcl为Python的飞信库写一个简单的GUI

[复制链接]

尚未签到

发表于 2015-4-20 13:29:48 | 显示全部楼层 |阅读模式
  Python的好处,就是类库超多,多到只有你想不到的库,而没有你想到的,他却没有的库。所以飞信,在Python下也有一个开源的类库,这个就是PyFetion,他自己有带一个Demo,不过是一个CGI的程序,没有窗口界面,于是用之前Delphi写的DxVcl为这个飞信库实现了一个简单的界面GUI。代码很简单,就是两个窗口,一个窗口是验证码输入的窗口,还有一个是主窗口。界面信息:
DSC0000.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[0]=='':
                        buddys[0]=i[4:4+9]
                for i in range(len(buddys)):
                                        s = "%-4d%-8s%-20s" % (i,status[buddys[buddys.keys()][2]].decode('gb2312').encode('utf8'),buddys[buddys.keys()][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而已,另外发送短信,也就只写了发送给自己而已。如果个人有需要的自行扩展一下吧,嘿嘿,完整代码下载

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58919-1-1.html 上篇帖子: 警惕python的*重复符 下篇帖子: 解决 Python.h:没有那个文件或目录 错误的方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表