基于Python制作短信发送程序
作者:虚坏叔叔 发布时间:2022-06-19 16:47:25
标签:Python,短信,发送
一、Python短信发送界面最后的效果
二、准备:注册腾讯云账号并配置短信功能
(1)注册腾讯云账号
登录腾讯云网址
(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)
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)
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
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);
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);
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);
四、完整源代码
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()
来源:https://blog.csdn.net/biggbang/article/details/127231278
0
投稿
猜你喜欢
- 如何制作一个倒计时的程序? 见下:<%CountdownDate = #1/1
- python爬虫用mongodb的原因:1、文档结构的存储方式简单讲就是可以直接存json,list2、不要事先定义”表”,随时可以创建3、
- 在网上查了部分资料但是发现粘上去的代码都存在问题,无奈只好自己修改了一下,代码如下: 如下代码能正常运行,都是网上查找资料最后拼凑总结出来的
- 字符替换是一个非常重要的功能,javascript 中有一个 String.replace( ),但是此方法有
- 晚上突然间看到大猫的头像在闪动,速度打开一看,发现他问,以前我写button标签的时候有没有写type属性,老实的我只有诚实地告诉他,我没写
- 案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造数据集特征展示1 GRE
- 现在Ajax是一个相当火的东西,那么Ajax是什么呢?我的理解Ajax就是一个工具,就是一个客户端的技术,不管用何种服务器端技术都可以用Aj
- 一、给定一个日期值,求出此日期所在星期的星期一和星期天的日期数据 例如给定一个日期 2010-09-01,求出它所在星期的星期一是2010-
- 昨天ie8正式发布了,偶也去下载了一个,感觉很爽, 还在美的时候,突然发现很多网页都出问题,更可气的是自己的网站编辑器eWebEditor也
- 小程序miniso的一个发布内容截图功能,话不多,先上代码wxml文件:<view class="cut-1-1 t-c {
- 这里列出了13种实现图片或网页内容 lightbox 效果的方法,大部分是链接到各种lightbox作者的英文页面,里面都有源代码下载。Th
- 事件的概念事件:指的是文档或者浏览器窗口中发生的一些特定交互瞬间。我们可以通过 * (或者处理程序)来预定事件,以便事件发生的时候执行相应的
- 首先要把php_iconv.dll和inconv.dll COPY到c:\winnt\system32下,直接上代码:<?define
- <% String st = ""; for(int i = 1; i <= 9;
- staytime.asp<% If Request.QueryString("time")&n
- 在my.cnf中设置了sql_mode='STRICT_TRANS_TABLES';却发现还是有非法数据的写入,比如无符号整
- asp压缩access数据库,具体asp代码见下: Class DatabaseTools &n
- 前段时间做一个小项目碰到了一个导航制作的方式然后突然想到曾经很久以前看到的梯形状的不规则导航,就尝试做了一下。结果碰到了几个问题,后来在同事
- 模板引擎的思想是来源于MVC(Model View Controller)模型,即模型层、视图层、控制器层。在Web端,模型层为数据库的操作
- 看代码: HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi