突然想到好久没动代码了,手痒痒,但是又不知道弄啥。想了想平时自己闲来无事都会买彩票,路过买两块,中不中全看天意。
但是经常买彩票的时候不知道选什么号码,犹豫的几分钟时间就浪费了,所以写了个彩票号码生成器,将软件打包,手点一下自动选号,并将号码发送到我的邮箱,其实初衷是想练习代码,这想法本来就不太实际,不过写都写了,就发出来给大伙学习学习。
文中涉及到的tkiner样式参考该博主博文:传送门
预览
邮件详情
邮件发送
import smtplib
import email.mime.multipart
import email.mime.text
# from flask_mail import Maildef send_qqemail(content):msg = email.mime.multipart.MIMEMultipart()msgFrom = '10617*****@qq.com' # 从该邮箱发送msgTo = '10617*****@qq.com' # 发送到该邮箱smtpSever = 'smtp.qq.com' # 163邮箱的smtp Sever地址smtpPort = '465'sqm = '**********' # 在登录smtp时需要login中的密码应当使用授权码而非账户密码msg['from'] = msgFrommsg['to'] = msgTomsg['subject'] = '今日彩票号码' # 邮件标题content = contenttxt = email.mime.text.MIMEText(content)msg.attach(txt)smtp = smtplib.SMTP_SSL(host='smtp.qq.com')smtp.connect(smtpSever, port=smtpPort)smtp.login(msgFrom, sqm)smtp.sendmail(msgFrom, msgTo, str(msg))smtp.quit()
号码生成
import random
import tkinter as tk
global num
num=0
#生成号码
def all_number_str():a=random.sample(range(1,34),6)b=random.sample(range(1,17),1)if b[0]<10:b='0'+str(b[0])else:b=str(b[0])strs=''for i in a:if i<10:strs+='0'+str(i)+' 'else:strs+=str(i)+' 'return strs+b
root = tk.Tk()
root.title('Lucky_number')
root.geometry('300x100')
root.overrideredirect(1) # 去除窗口边框
var1 = tk.StringVar()
l = tk.Label(root, textvariable=var1,fg='red',font=('Arial', 15), width=0, height=2)
l.place(x=46,y=0)
var2 = tk.StringVar()
l2 = tk.Label(root, textvariable=var2,fg='blue',font=('Arial', 15), width=0, height=2)
l2.place(x=216,y=0)
def hit_me():global num,F5,fastif num>60:#发送邮件send_qqemail(all_number_str())num=0else:var1.set(all_number_str()[0:17])var2.set(all_number_str()[18:21])F5=root.after(50,hit_me)num+=1b = tk.Button(root, text='Hit me', font=('Arial', 12), width=10, height=1, command=hit_me)
b.place(x=100,y=60)
#绑定任何按钮退出程序
def myquit(*args):root.destroy()
def StartMove(event):global x, yx = event.xy = event.y
def StopMove(event):global x, yx = Noney = None
def OnMotion(event):global x, ydeltax = event.x - xdeltay = event.y - yroot.geometry("+%s+%s" % (root.winfo_x() + deltax, root.winfo_y() + deltay))root.update()
# print(event.x,event.y,root.winfo_x(),root.winfo_y(),root.winfo_width(),root.winfo_height())root.bind("<ButtonPress-3>", StartMove) #监听右键按下操作响应函数
root.bind("<ButtonRelease-3>", StopMove) #监听右键松开操作响应函数
root.bind("<B3-Motion>", OnMotion)
root.bind("<Any-KeyPress>", myquit)#监听键盘按下root.mainloop()