Python tkinter多进程多线程前邮箱,再用pyinstaller编译成exe

阅读:1171 2019-03-20 14:08:58 来源:新网

写博客记录一下python用tkinter多进程线程写成的邮箱应用,只是一个简单的应用,尝试进程调度,并用pyinstaller打包成exe

注意:

1、平台环境win7、python3.6、pyinstaller3.3(安装方法,百度有,这里不加说明,windows下python3的pyinstaller会出错,解决办法,百度)

2、windows下多进程,在if__name__=='__main__':里面去启动才不会出错

3、tkinter调用mainloop()函数应在主线程的最后一行,因为mainloop会进入屏幕显示循环,mainloop()之后的代码会在ui界面退出之后再执行(已经没意义了),

所以重点:后台进程,后台线程,要在mainloop之前去启动

4、pyinstaller打包python多进程请加以下这行

multiprocessing.freeze_support()

5、以上注意在代码注释有

从上之下依次是label:作用是显示,发送成功,失败,以及找不到附件等信息

第一个entry是输入主题

第二个entry是输入正文(这里随便用了entry,用text更合适)

第三个entry输入附件的文件位置

button就是发送

tkinter进程代码(继承了threading.thread)

fromtkinterimport*importmultiprocessingfromsend_mailimportsend_my_mail#引入发送邮件importthreadingclassmy_gui(threading.thread):"""docstringformy_gui"""def__init__(self,queue_1,queue_2,event_1):super(my_gui,self).__init__()self.queue_1=queue_1self.queue_2=queue_2self.root=tk()self.root.title="email"self.label_1=label(self.root,text='info')self.entry_1=entry(self.root,)self.entry_2=entry(self.root,)self.entry_3=entry(self.root,)#button的command调用函数需要参数时用lambdaself.button_1=button(self.root,text='send',command=lambda:self.button_click(queue_1,event_1))self.label_1.pack()self.entry_1.pack()self.entry_2.pack()self.entry_3.pack()self.button_1.pack()#self.root.mainloop()defbutton_click(self,queue,event_1):ifnotself.entry_1.get()=='':queue.put(self.entry_1.get())#获取三个输入框内容依次进入队列发送给后台进程queue.put(self.entry_2.get())queue.put(self.entry_3.get())event_1.set()self.label_1['text']='sendingemail'#多线程等待后台进程返回消息,防止ui卡顿defrun(self):self.button_1['text']='send'whiletrue:ifnotself.queue_2.empty():info=self.queue_2.get()ifinfo=='succeed':self.label_1['text']='succeed'elifinfo=='failure':self.label_1['text']='failure'else:self.label_1['text']='filenotfound'defback_process(queue_1,queue_2,event_1):whiletrue:event_1.wait()subject=queue_1.get()#后台进程获取ui进程“主题”输入框内容body=queue_1.get()#后台进程获取ui进程“正文”输入框内容img=queue_1.get()#附件flage_1=send_my_mail(subject,body,img)#调用发送邮件函数queue_2.put(flage_1)#将发送邮件函数的返回发送给ui进程event_1.clear()if__name__=='__main__':#多线程多进程都必须在mainloop之前去执行multiprocessing.freeze_support()#在windows下编译需要加这行queue_1=multiprocessing.queue()#用来ui进程向后台发送邮件进程发送消息queue_2=multiprocessing.queue()#用来后台进程向ui进程发送消息event_1=multiprocessing.event()#控制后台进程是否阻塞t=multiprocessing.process(target=back_process,args=(queue_1,queue_2,event_1))t.daemon=truet.start()#要先于mainloop调用start不然进程不会启动my_gui=my_gui(queue_1,queue_2,event_1)#gui之后的代码不执行my_gui.daemon=truemy_gui.start()#要先于mainloop调用start不然线程不会启动my_gui.root.mainloop()#mainloop必须要在最后去执行,相当于while阻塞发送邮件的python文件

importsmtplib,sysfromemail.mime.textimportmimetextfromemail.mime.multipartimportmimemultipartfromemail.headerimportheaderdefsend_my_mail(subject,body,img):form_addr="你的邮箱@163.com"my_fassword='你的邮箱密码'to_addr=["你要发送的邮箱@qq.com"]msg=mimemultipart()msg['from']=header(form_addr)msg['to']=header("你要发送的邮箱@qq.com")msg['subject']=header(subject,"utf-8")body=mimetext(body,'plain','utf-8')msg.attach(body)try:att1=mimetext(open(img,'rb').read(),'base64','utf-8')exceptexceptionase:return'filenotfound'att1['content-type']='application/octet-stream'att1['content-disposition']='attachment;filename=%s'%imgmsg.attach(att1)try:smtpobj=smtplib.smtp()smtpobj.connect('smtp.163.com')smtpobj.login(form_addr,my_fassword)smtpobj.sendmail(form_addr,to_addr,msg.as_string())#print('succeed')return'succeed'exceptexceptionase:print(str(e))return'failure'

上一篇: java实现邮件发送
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服