我有这几行代码来tkinter
发送邮件,我想做的是在发送邮件时用sending in progress...
配置标签,但标签没有配置,直到在配置标签之前输出错误。
我希望标签配置邮件正在发送,然后在完成后配置mail sent
from tkinter import messagebox
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import socket
import tkinter as tk
def my_second_mail():
try:
l.config(text="sending in progress...")
FROMADDR = "my@gmail.com"
PASSWORD = "22@201"
TOADDR = ['@yahoo.com', '@gmail.com']
CCADDR = ['fkfkf@gmail.com']
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = 'BACK UP FILES'
msg['From'] = FROMADDR
msg['To'] = ', '.join(TOADDR)
msg['Cc'] = ', '.join(CCADDR)
body = "mail test"
msg.attach(MIMEText(body, 'plain'))
filename = ['facebook.png']
for n in filename:
# l.config(text="sending in progress...")
print(n)
attachment = open(str(n), 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= " + n)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(FROMADDR, PASSWORD)
server.sendmail(FROMADDR, TOADDR + CCADDR, msg.as_string())
server.quit()
messagebox.showinfo("success", "mail sent", parent=root)
l.config(text="mail sent...")
except socket.gaierror:
messagebox.showerror("no internet", "no internet connectivity")
root = tk.Tk()
root.geometry("600x400")
canvas = tk.Canvas(root, width=200, height=200, background="dodger blue")
canvas.pack(fill="both", expand=True)
l = tk.Label(canvas, bg="dodger blue", fg="white")
l.place(x=250, y=20)
b= tk.Button(canvas, text="send mail", command=my_second_mail, width=25, fg='dodger blue')
b.place(x=220, y=330)
root.mainloop()
添加update
调用。
l.config(text="sending in progress...")
l.update()
这将执行您想要的操作,但在发送过程中 GUI 仍将被锁定。要做到这一点,您应该将发送过程移动到新线程,并通过 StringVar 更新标签。完全未经测试的猜测:
from tkinter import messagebox
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import socket
import tkinter as tk
from threading import Thread
def my_second_mail():
try:
message.set("sending in progress...")
FROMADDR = "my@gmail.com"
PASSWORD = "22@201"
TOADDR = ['@yahoo.com', '@gmail.com']
CCADDR = ['fkfkf@gmail.com']
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = 'BACK UP FILES'
msg['From'] = FROMADDR
msg['To'] = ', '.join(TOADDR)
msg['Cc'] = ', '.join(CCADDR)
body = "mail test"
msg.attach(MIMEText(body, 'plain'))
filename = ['facebook.png']
for n in filename:
message.set("sending in progress...")
print(n)
attachment = open(str(n), 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= " + n)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(FROMADDR, PASSWORD)
server.sendmail(FROMADDR, TOADDR + CCADDR, msg.as_string())
server.quit()
messagebox.showinfo("success", "mail sent", parent=root)
message.set("mail sent...")
except socket.gaierror:
messagebox.showerror("no internet", "no internet connectivity")
def start_mail_send():
t = Thread(target=my_second_mail)
t.start()
root = tk.Tk()
root.geometry("600x400")
canvas = tk.Canvas(root, width=200, height=200, background="dodger blue")
canvas.pack(fill="both", expand=True)
message = tk.StringVar()
l = tk.Label(canvas, textvariable=message, bg="dodger blue", fg="white")
l.place(x=250, y=20)
b= tk.Button(canvas, text="send mail", command=start_mail_send, width=25, fg='dodger blue')
b.place(x=220, y=330)
root.mainloop()