如何检索条目和文本小部件的内容,并使用python将其分配给tkinter中的变量

时间:2013-07-16 09:55:02

标签: python tkinter

我有以下代码,我用python创建了基于gui的电子邮件客户端。我使用了tkinter。我希望用户在条目小部件中写入的文本被分配给变量发送者,密码,接收者,消息。 我已经使用了入口小部件来获取消息的发送者,密码,接收者和文本小部件的值。如何做?我为此尝试了很多方法(比如get方法,textvariable方法)但是没有执行.I我是一个新手,因此更喜欢一个不涉及课程的答案。请尽快回复!很紧急。谢谢你的帮助。我只是告诉你我做了什么,以便为变量发送者分配价值,但我想要所有这些四个变量。

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()

1 个答案:

答案 0 :(得分: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()

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

P.S。我抛弃了你的一些代码,这样我就能更好地处理你的问题。