• Python制作短信发送程序


    作者:虚坏叔叔
    博客:https://xuhss.com

    早餐店不会开到晚上,想吃的人早就来了!😄

    Python制作短信发送程序

    [(img-xeaVDvJR-1665309505450)(02.assets/1.png)]

    一、Python短信发送界面最后的效果

    图片

    二、准备:注册腾讯云账号并配置短信功能

    (1)注册腾讯云账号

    登录腾讯云网址 https://cloud.tencent.com/ 注册。

    (2)获取AppID、AppKey

    在短信功能页面下,从应用管理>应用列表,获取ID、Key。

    (3)创建签名

    在短信功能页面下,进入国内短信>签名管理,创建签名。

    (4)创建正文模板

    在短信功能页面下,进入国内短信>正文模板管理,创建模版。并获取模板ID备用。

    三.初始化短信发送程序窗口

    3.1初始化窗口菜单

    菜单具备打开手机号码文件、保存记录、查看版本等功能。

        menu=tkinter.Menu(root)
        submenu1 = tkinter.Menu(menu, tearoff=0)
        submenu1.add_command(label='打开', command=open_file)
        submenu1.add_command(label='保存', command=save_file)
        menu.add_cascade(label='文件',menu=submenu1)
        submenu3 = tkinter.Menu(menu, tearoff=0)    
        submenu3.add_command(label='版本信息', command=Introduction)
        menu.add_cascade(label='帮助',menu=submenu3)
        root.config(menu=menu)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.2初始化窗口控件

    控件包括号码输入框、发送信息按钮,记录显示框。

        global text1,text2
        label1 = tkinter.Label(root, text="手机号码:", font=("微软雅黑", 18))
        label1.place(x=30,y=32)
        text1 = tkinter.Text(root, wrap = 'none', font=("微软雅黑", 18))
        text1.place(x=30+120,y=30, width=520-120-100, height=40)
        button=tkinter.Button(root, text='发送信息',width=10, height=20, bg='gray', fg='white', font=("微软雅黑", 12),command=send_Button)
        button.place(x=480,y=30,width=70, height=40)
        sx = tkinter.Scrollbar(root,orient = tkinter.HORIZONTAL)
        sx.pack(side = tkinter.BOTTOM,fill = tkinter.X)
        sy = tkinter.Scrollbar(root)
        sy.pack(side = tkinter.RIGHT,fill = tkinter.Y)     
        text2 = tkinter.Text(root, yscrollcommand = sy.set, xscrollcommand = sx.set, wrap = 'none', font=("微软雅黑", 10))
        text2.place(x=30,y=100, width=520, height=400)
        text2.config(wrap=tkinter.WORD)
        text2.see(tkinter.END);
        sx.config(command = text2.xview) 
        sy.config(command = text2.yview)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.3编写事件触发程序

    3.3.1文件打开
    def open_file():
        global file_path,phone_numbers,flag
        file_path = filedialog.askopenfilename()
        if file_path is not "":
            data=pandas.read_excel(file_path)
            phone = data['号码'].tolist()
            for i in range(len(phone)):
                phone_numbers.append(str(phone[i]))
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"打开文件成功!"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"文件路径为:"+file_path+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"文件内容如下:"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,data, '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"\n", '\n')
            text2.see(tkinter.END);
            flag = 1
        else:
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"您未打开文件!"+"\n", '\n')
            text2.see(tkinter.END);
            flag = 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    3.3.2文件保存
    def save_file():
        file=open("recorde.txt","a+")
        content=str(text2.get("0.0", "end"))
        file.write(content)
        file.close()
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"保存记录到recorde.txt成功!"+"\n", '\n')
        text2.see(tkinter.END);
        tkinter.messagebox.showinfo('提示','保存记录到recorde.txt成功!')
        text2.see(tkinter.END);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    3.3.3帮助菜单
    def Introduction():
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"版本信息:短信息通知程序 V1.0"+"\n", '\n')
        text2.see(tkinter.END);
        tkinter.messagebox.showinfo('版本信息' ,'短信息通知程序 V1.0')
        text2.see(tkinter.END);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3.3.4发送按钮
    def send_Button():
        global flag,phone_numbers
        appid = "你的appid"
        appkey = "你的appkey"
        template_id = "你的模板ID"
        sms_sign = "你的公众号名称"
        params = []
        ssl._create_default_https_context = ssl._create_unverified_context  
        ssender = SmsSingleSender(appid, appkey)    
        txt1 = str(text1.get("0.0", "end")).replace('\n', '')
        if flag==0:
            if ',' in txt1:
                phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
            elif ',' in txt1:
                phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
            else:
                phone_numbers=[]
                phone_numbers.append(txt1)
        else:
            flag = 0
        count=0
        for l in phone_numbers:
            count=count+len(str(l))
        if count%11==0:
            result = ""
            for i in range(len(phone_numbers)):
                try:
                    result = ssender.send_with_param(86, phone_numbers[i],template_id, params, sign=sms_sign, extend="", ext="") 
                except HTTPError as e:  
                    result=e
                except Exception as e:  
                    result=e
                text2.insert(tkinter.END,"*********************************"+"\n", '\n')
                text2.see(tkinter.END);
                text2.insert(tkinter.END,"信息发送至手机号:"+"\n"+str(phone_numbers[i])+"\n")
                text2.see(tkinter.END);
                text2.insert(tkinter.END,"信息发送返回结果:"+"\n")
                text2.see(tkinter.END);
                text2.insert(tkinter.END,str(result)+"\n", '\n')
                text2.see(tkinter.END);
                if result['errmsg']=='OK':
                    text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】成功!"+"\n")
                    text2.see(tkinter.END);
                else:
                    text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】失败!"+"\n")
                    text2.see(tkinter.END);
        else:
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"手机号码格式不正确"+"\n", '\n')
            text2.see(tkinter.END);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    四、完整源代码

    import tkinter
    import tkinter.messagebox
    from tkinter import filedialog
    import pandas
    import ssl
    from qcloudsms_py import SmsSingleSender  
    from qcloudsms_py.httpclient import HTTPError 
    
    def open_file():
        global file_path,phone_numbers,flag
        file_path = filedialog.askopenfilename()
        if file_path is not "":
            data=pandas.read_excel(file_path)
            phone = data['号码'].tolist()
            for i in range(len(phone)):
                phone_numbers.append(str(phone[i]))
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"打开文件成功!"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"文件路径为:"+file_path+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"文件内容如下:"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,data, '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"\n", '\n')
            text2.see(tkinter.END);
            flag = 1
        else:
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"您未打开文件!"+"\n", '\n')
            text2.see(tkinter.END);
            flag = 0
        
    def save_file():
        file=open("recorde.txt","a+")
        content=str(text2.get("0.0", "end"))
        file.write(content)
        file.close()
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"保存记录到recorde.txt成功!"+"\n", '\n')
        text2.see(tkinter.END);
        tkinter.messagebox.showinfo('提示','保存记录到recorde.txt成功!')
        text2.see(tkinter.END);
            
    def Introduction():
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"版本信息:短信息通知程序 V1.0"+"\n", '\n')
        text2.see(tkinter.END);
        tkinter.messagebox.showinfo('版本信息' ,'短信息通知程序 V1.0')
        text2.see(tkinter.END);
        
    
    def send_Button():
        global flag,phone_numbers
        appid = "你的appid"
        appkey = "你的appkey"
        template_id = "你的模板ID"
        sms_sign = "你的公众号名称"
        params = []
        ssl._create_default_https_context = ssl._create_unverified_context  
        ssender = SmsSingleSender(appid, appkey)    
        txt1 = str(text1.get("0.0", "end")).replace('\n', '')
        if flag==0:
            if ',' in txt1:
                phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
            elif ',' in txt1:
                phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
            else:
                phone_numbers=[]
                phone_numbers.append(txt1)
        else:
            flag = 0
        count=0
        for l in phone_numbers:
            count=count+len(str(l))
        if count%11==0:
            result = ""
            for i in range(len(phone_numbers)):
                try:
                    result = ssender.send_with_param(86, phone_numbers[i],template_id, params, sign=sms_sign, extend="", ext="") 
                except HTTPError as e:  
                    result=e
                except Exception as e:  
                    result=e
                text2.insert(tkinter.END,"*********************************"+"\n", '\n')
                text2.see(tkinter.END);
                text2.insert(tkinter.END,"信息发送至手机号:"+"\n"+str(phone_numbers[i])+"\n")
                text2.see(tkinter.END);
                text2.insert(tkinter.END,"信息发送返回结果:"+"\n")
                text2.see(tkinter.END);
                text2.insert(tkinter.END,str(result)+"\n", '\n')
                text2.see(tkinter.END);
                if result['errmsg']=='OK':
                    text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】成功!"+"\n")
                    text2.see(tkinter.END);
                else:
                    text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】失败!"+"\n")
                    text2.see(tkinter.END);
        else:
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"手机号码格式不正确"+"\n", '\n')
            text2.see(tkinter.END);
    
    
        
    def init_frame(root):   
        menu=tkinter.Menu(root)
        submenu1 = tkinter.Menu(menu, tearoff=0)
        submenu1.add_command(label='打开', command=open_file)
        submenu1.add_command(label='保存', command=save_file)
        menu.add_cascade(label='文件',menu=submenu1)
        submenu3 = tkinter.Menu(menu, tearoff=0)    
        submenu3.add_command(label='版本信息', command=Introduction)
        menu.add_cascade(label='帮助',menu=submenu3)
        root.config(menu=menu)
        global text1,text2
        label1 = tkinter.Label(root, text="手机号码:", font=("微软雅黑", 18))
        label1.place(x=30,y=32)
        text1 = tkinter.Text(root, wrap = 'none', font=("微软雅黑", 18))
        text1.place(x=30+120,y=30, width=520-120-100, height=40)
        button=tkinter.Button(root, text='发送信息',width=10, height=20, bg='gray', fg='white', font=("微软雅黑", 12),command=send_Button)
        button.place(x=480,y=30,width=70, height=40)
        sx = tkinter.Scrollbar(root,orient = tkinter.HORIZONTAL)
        sx.pack(side = tkinter.BOTTOM,fill = tkinter.X)
        sy = tkinter.Scrollbar(root)
        sy.pack(side = tkinter.RIGHT,fill = tkinter.Y)     
        text2 = tkinter.Text(root, yscrollcommand = sy.set, xscrollcommand = sx.set, wrap = 'none', font=("微软雅黑", 10))
        text2.place(x=30,y=100, width=520, height=400)
        text2.config(wrap=tkinter.WORD)
        text2.see(tkinter.END);
        sx.config(command = text2.xview) 
        sy.config(command = text2.yview)
        root.update()
    
    if __name__=="__main__":
        global flag
        flag = 0
        global phone_numbers
        phone_numbers = []
        root = tkinter.Tk()
        root.title("短信息发送程序")
        root.geometry('600x520')
        init_frame(root)
        root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150

    💬 往期优质文章分享

    🚀 优质教程分享 🚀

    • 🎄如果感觉文章看完了不过瘾,可以来我的其他 专栏 看一下哦~
    • 🎄比如以下几个专栏:Python实战微信订餐小程序、Python量化交易实战、C++ QT实战类项目 和 算法学习专栏
    • 🎄可以学习更多的关于C++/Python的相关内容哦!直接点击下面颜色字体就可以跳转啦!
    学习路线指引(点击解锁)知识定位人群定位
    🧡 Python实战微信订餐小程序 🧡进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
    💛Python量化交易实战 💛入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统
    ❤️ C++ QT结合FFmpeg实战开发视频播放器❤️难度偏高分享学习QT成品的视频播放器源码,需要有扎实的C++知识!
    💚 游戏爱好者九万人社区💚互助/吹水九万人游戏爱好者社区,聊天互助,白嫖奖品
    💙 Python零基础到入门 💙Python初学者针对没有经过系统学习的小伙伴,核心目的就是让我们能够快速学习Python的知识以达到入门

    🚀 资料白嫖,温馨提示 🚀

    关注下面卡片即刻获取更多编程知识,包括各种语言学习资料,上千套PPT模板和各种游戏源码素材等等资料。更多内容可自行查看哦!

    请添加图片描述

  • 相关阅读:
    【Java每日一题】— —第二十六题:编程定义一个经理类Manager。(2023.10.10)
    HTML简单实现v-if与v-for与v-model
    网址浏览历史记录怎么查?分享四个特别管用的方法!一分钟就学会!
    Tableau4——标靶图,甘特图,瀑布图
    使用dockerfile部署springboot应用
    【python】爬虫入门相关
    js生成图片的多边形科技感效果
    【图论C++】Floyd算法(多源最短路径长 及 完整路径)
    剑指offer之链表
    思考-贪心算法
  • 原文地址:https://blog.csdn.net/huangbangqing12/article/details/127231278