好吧,我有以下代码,用于使用 python 创建基于 gui 的电子邮件客户端。我使用了 tkinter。我希望用户在条目小部件中写入的文本被分配给变量发件人、密码、收件人、消息。我已经使用条目小部件来获取消息的发送者、密码、接收者和文本小部件的值。如何做到这一点?我为此尝试了很多方法(如 get 方法、textvariable 方法)但它没有被执行。我我是一个新手,因此更喜欢不涉及课程的答案。请尽快回复!它很紧急。感谢您的帮助。我只是向您展示我在为变量发件人分配值的情况下所做的事情,但我希望所有的四个变量。
import smtplib
from Tkinter import *
import tkMessageBox
def Composemail(sender,password,receivers,message):
try:
server = smtplib.SMTP()
server.connect('smtp.gmail.com',587) # for eg. host = 'smtp.gmail.com', port = 587
server.ehlo()
server.starttls()
server.login(sender, password)
server.sendmail(sender, receivers, message)
#smtpObj = smtplib.SMTP_SSL('smtp.gmail.com',587)
#smtpObj.sendmail(sender, receivers, message)
tkMessageBox.showinfo("Sending Mail information","Mail sent.")
except smtplib.SMTPException, error:
tkMessageBox.showinfo("Sending Mail information","Sending Mail failed.Try again later.")
a=Tk()
a.title("MailsNow-A new place for sending emails")
a.geometry("1000x700")
b=Label(a,fg="Purple",text="From")
b.pack()
sender=StringVar() #the problem starts here
c=Entry(a,bd=5,width=100,textvariable=sender)
c.pack()
d=Label(a,fg="Purple",text="Password")
d.pack()
e=Entry(a,bd=5,width=100,show="*")
e.pack()
password='abc'
f=Label(a,fg="Purple",text="To")
f.pack()
receivers = 'abc@gmail.com'
g=Entry(a,bd=5,width=100)
g.pack()
h=Label(a,fg="Purple",text="Subject")
h.pack()
i=Entry(a,bd=5,width=100)
i.pack()
j=Label(a,fg="Purple",text="Type your email here")
j.pack()
k=Text(a,bd=5,height=20,width=100)
k.pack()
message = """From: From Person <abc@gmail.com>#I want to know how to do the same for text #widget too
To: To Person <xyz@gmail.com>
Subject: SMTP e-mail test
Hi , now I can send you emails using this......hurrah1!using Sending mail transfer protocol by a python
code!!!!
"""
l=Button(a,
text="Sendmail",bg="Purple",activebackground="Yellow",
command=lambda:Composemail(sender,password,receivers,message))
l.pack()
a.mainloop()