Tkinter StringVar 发送邮件错误 SMTPlib
Tkinter StringVar Send mail error SMTPlib
我正在制作一个 Tkinter 应用程序来发送一封简短的电子邮件。
完整代码:
from Tkinter import *
from smtplib import *
from time import sleep
root=Tk()
root.wm_title("Gmail short message sender")
w = 500 # width for the Tk root
h = 500 # height for the Tk root
# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
def send():
e1_var.get()
e2_var.get()
e3_var.get()
try:
smtpObj = SMTP('smtp.gmail.com', "587")
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login("123@gmail.com", "password")
smtpObj.sendmail(sender1, receivers1, msg1)
l1=Label(text="Sent").grid()
sleep(1)
l1.destroy()
except SMTPException:
l2=Label(text="Error").grid()
raise
e1_var=StringVar()
e2_var=StringVar()
e3_var=StringVar()
sender = e1_var
receivers = [e2_var]
msg = e3_var
sender1 = '%s' % (sender)
receivers1 = '[%s]'% (receivers)
msg1 = '%s' % (msg)
e1=Entry(root, textvariable=e1_var).grid()
e2=Entry(root, textvariable=e2_var).grid()
e3=Entry(root, textvariable=e3_var).grid()
b1=Button(text="Send", command=send).grid()
root.mainloop()
问题:
问题出在我的 StringVar
个实例上。首先 StringVar
个实例出现了这个错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
return self.func(*args)
File "Desktop/Python Scripts/Email.py", line 32, in send
smtpObj.sendmail(sender1, receivers1, msg1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 724, in sendmail
esmtp_opts.append("size=%d" % len(msg))
AttributeError: StringVar instance has no attribute '__len__'
但我通过在
中添加此部分来解决此问题
sender1 = sender
receivers1 = receivers
msg1 = msg
并将 smtpObj.sendmail(sender, receivers, msg)
更改为 smtpObj.sendmail(sender1, receivers1, msg1)
。我现在收到此错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
return self.func(*args)
File "Desktop/Python Scripts/Email.py", line 32, in send
smtpObj.sendmail(sender1, receivers1, msg1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'[[<Tkinter.StringVar instance at 0x103e8be60>]]': (555, '5.5.2 Syntax error. yi8sm12824416pab.22 - gsmtp')}
这是由于引号引起的语法错误吗?如果是这样,我该如何解决第一个错误?
这个(和其他类似的代码)不正确:
sender = e1_var
receivers = [e2_var]
msg = e3_var
你需要改成这样:
sender = e1_var.get()
receivers = [e2_var.get()]
msg = e3_var.get()
但这不是您唯一的问题。此代码在用户有机会输入任何内容之前 运行,因此结果将全部为空字符串。您需要等待调用 .get()
方法,直到您准备好使用它们(奇怪的是,您已经在 send
内部执行)。
我正在制作一个 Tkinter 应用程序来发送一封简短的电子邮件。
完整代码:
from Tkinter import *
from smtplib import *
from time import sleep
root=Tk()
root.wm_title("Gmail short message sender")
w = 500 # width for the Tk root
h = 500 # height for the Tk root
# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
def send():
e1_var.get()
e2_var.get()
e3_var.get()
try:
smtpObj = SMTP('smtp.gmail.com', "587")
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login("123@gmail.com", "password")
smtpObj.sendmail(sender1, receivers1, msg1)
l1=Label(text="Sent").grid()
sleep(1)
l1.destroy()
except SMTPException:
l2=Label(text="Error").grid()
raise
e1_var=StringVar()
e2_var=StringVar()
e3_var=StringVar()
sender = e1_var
receivers = [e2_var]
msg = e3_var
sender1 = '%s' % (sender)
receivers1 = '[%s]'% (receivers)
msg1 = '%s' % (msg)
e1=Entry(root, textvariable=e1_var).grid()
e2=Entry(root, textvariable=e2_var).grid()
e3=Entry(root, textvariable=e3_var).grid()
b1=Button(text="Send", command=send).grid()
root.mainloop()
问题:
问题出在我的 StringVar
个实例上。首先 StringVar
个实例出现了这个错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
return self.func(*args)
File "Desktop/Python Scripts/Email.py", line 32, in send
smtpObj.sendmail(sender1, receivers1, msg1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 724, in sendmail
esmtp_opts.append("size=%d" % len(msg))
AttributeError: StringVar instance has no attribute '__len__'
但我通过在
中添加此部分来解决此问题sender1 = sender
receivers1 = receivers
msg1 = msg
并将 smtpObj.sendmail(sender, receivers, msg)
更改为 smtpObj.sendmail(sender1, receivers1, msg1)
。我现在收到此错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
return self.func(*args)
File "Desktop/Python Scripts/Email.py", line 32, in send
smtpObj.sendmail(sender1, receivers1, msg1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'[[<Tkinter.StringVar instance at 0x103e8be60>]]': (555, '5.5.2 Syntax error. yi8sm12824416pab.22 - gsmtp')}
这是由于引号引起的语法错误吗?如果是这样,我该如何解决第一个错误?
这个(和其他类似的代码)不正确:
sender = e1_var
receivers = [e2_var]
msg = e3_var
你需要改成这样:
sender = e1_var.get()
receivers = [e2_var.get()]
msg = e3_var.get()
但这不是您唯一的问题。此代码在用户有机会输入任何内容之前 运行,因此结果将全部为空字符串。您需要等待调用 .get()
方法,直到您准备好使用它们(奇怪的是,您已经在 send
内部执行)。