3

好吧,我有以下代码,用于使用 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()
4

1 回答 1

1

在您的特定情况下,您不需要使用StringVar. 这应该是你想要的:

import smtplib
from Tkinter import *
import tkMessageBox

def Composemail(sender,password,receivers,message):
    try:
        server = smtplib.SMTP()
        server.connect('smtp.gmail.com',587)
        server.ehlo()
        server.starttls()
        server.login(sender, password)
        server.sendmail(sender, receivers, message)
        tkMessageBox.showinfo("Sending Mail information","Mail sent.")
    # Just a tip, "error" isn't defined yet so it will blow up.
    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()
c=Entry(a,bd=5,width=100)
c.pack()
d=Label(a,fg="Purple",text="Password")
d.pack()
e=Entry(a,bd=5,width=100,show="*")
e.pack()
f=Label(a,fg="Purple",text="To")
f.pack()
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()

def getstuff():
    # You can retrieve the text entered into an entrybox using the get method
    sender=c.get()
    password=e.get()
    receivers=g.get()
    subject=i.get()
    # Since textboxes are multiline, I have to tell it what text to get.
    # 0.0,END is saying "get everything from start to finish".
    message=k.get(0.0,END)

    # One thing I noted was that you are not sending the subject to
    # Composemail.  Is this what you want?
    Composemail(sender,password,receivers,message)

l=Button(a, text="Sendmail",bg="Purple",activebackground="Yellow",
command=getstuff)
l.pack()

a.mainloop()

当您单击“发送邮件”按钮时,您在应用程序中输入的所有内容都会被收集并发送到Composemail. 从那里,你可以用它做任何你想做的事情。

ps 我扔掉了你的一些代码,这样我就可以更好地解决你的问题。

于 2013-07-16T15:35:51.637 回答