user2504528
user2504528

Reputation: 53

How to retrieve the contents of entry and text widget and assign it to a variable in tkinter using python

Well I have the following code which I wrote for creating a gui based email client using python.I used tkinter.I want that the text the user writes in the entry widget gets assigned to the variables sender,password,receiver,message. I have used entry widget for getting the values of sender,password,receiver and text widget for the message .How to do so?I tried many methods(like the get method,the textvariable method) for this but its not getting executed.I am a newbie and hence would prefer an answer which does not involve classes.Please reply soon!Its urgent.Thanks for your help.I am just showing you what I did in case of assigning value to variable sender but i want this for all the four variables.

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 = '[email protected]'
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 <[email protected]>#I want to know how to do the same for text #widget too
To: To Person <[email protected]>
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()

Upvotes: 3

Views: 538

Answers (1)

user2555451
user2555451

Reputation:

In your specific case, you don't need to use StringVar. This should be what you want:

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

When you click the "Sendmail" button, everything you entered into the application is gathered and sent to Composemail. From there, you can do whatever you want with it.

p.s. I threw out some of your code so I could work with your problem better.

Upvotes: 1

Related Questions