Python制作短信发送程序

01

Python制作短信发送程序

在上个推文中,梅朵给大家介绍了Python发送短信的实现方法。

今天,梅朵继续给大家演示:Python制作短信发送程序的编程实现。

Python短信发送界面

实现方法

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

(1)注册腾讯云账号

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

(2)获取AppID、AppKey

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

(3)创建签名

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

(4)创建正文模板

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

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

(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)`

(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)`

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

(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);

(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);

(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);

完整源代码

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()

公众号:实用办公编程技能

微信号:Excel-Python

欢迎留言!

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

扫码关注,更多精彩

公众号:实用办公编程技能

_1.__Python用词云图分析《庆余年》_剧评

_2._Python几行代码制作Gif动图

3_._Python制作一个简易计算器

4_.__Python制作二维码生成器_

5_._用Python控制摄像头录制视频

6_._用Python玩转视频播放__

7.用Python制作照片阅读器

8.用Python实现文本自动播读

9.Python制作一个简易时钟

10.Python代码实现手写数字识别

11.用Python轻松进行图像文本识别

12.用Python做小说词频分析图

点击下方安全链接前往获取

CSDN大礼包:《Python入门&进阶学习资源包》免费分享

👉Python实战案例👈

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

图片

图片

👉Python书籍和视频合集👈

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

图片

👉Python副业创收路线👈

图片

这些资料都是非常不错的,朋友们如果有需要《Python学习路线&学习资料》,点击下方安全链接前往获取

CSDN大礼包:《Python入门&进阶学习资源包》免费分享

本文转自网络,如有侵权,请联系删除。

  • 22
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值