0

I am currently working on a python tkinter application that allows the user to send an email to a user using a GUI window. I am running on python 3.4.2, and have created a [log in] window and a [compose message] window. Within my program i have created many entries where the user enters data, one for email, password, message, subject, to, and etc. I then have a function print() out all the data that the user has entered. The only problem is that only 3/6 entry results are showing up in the console. Can someone please look at my code and tell me what is wrong. My friends please do not flag my question, i'm just trying to learn why this isn't working so i can work on this and publish it online, if you want i can credit you in my work if that's what you want ;) Below i have provided a screen shot of the output in the console, and the 2 different windows i've created.

The Code:

#-->Project name : Email Buddy
#-->Project start date : Monday, August 24th, 2015.
#-->Project written by : Pamal Mangat.
#-->Project end date :

#Imports
from PIL import Image, ImageTk
import webbrowser
import email
import sys
import smtplib
import tkinter

def sendMail(recipient, sender, subject, message, password):
    #Function sends the email.
    msg = email.message_from_string(message)
    msg['From'] = sender
    msg['To'] = recipient
    msg['Subject'] = subject
    connect = smtplib.SMTP("smtp.live.com",587)
    connect.ehlo()
    connect.starttls() 
    connect.ehlo()
    connect.login(password, password)
    connect.sendmail(sender, recipient, msg.as_string())
    connect.quit()

def root():

    def testInfo(email, password, sender, reciever, message, subject):
        #Function checks if all info is entered.
        print("Email : " + email)
        print("Password : " + password)
        print("From : " + sender)
        print("To : " + reciever)
        print("Message : " + message)
        print("Subject : " + subject)

    #Created a send message (compose message) window (root = Compose Message) >>> User is redirected to this page after signing in.
    root = tkinter.Tk()
    root.title("Email Buddy | Compose Message")

    #Root text
    root_text1 = tkinter.Label(root, text="To", font="Bizon 20 bold", fg="Red")
    root_text1.place(x=20, y=50)

    root_text2 = tkinter.Label(root, text="Subject", font="Bizon 20 bold", fg="Red")
    root_text2.place(x=20, y=145)

    root_text3 = tkinter.Label(root, text="Message", font="Bizon 20 bold", fg="Red")
    root_text3.place(x=20, y=235)

    #Root entries
    to_Variable = tkinter.StringVar()
    to_Entry = tkinter.Entry(root, bd=4, width=28, textvariable=to_Variable, font="Helvetica 16 italic")
    to_Entry.focus()
    to_Entry.place(x=20, y=95)

    subject_Variable = tkinter.StringVar()
    subject_Entry = tkinter.Entry(root, bd=4, width=28, textvariable=subject_Variable, font="Helvetica 16 italic")
    subject_Entry.focus()
    subject_Entry.place(x=20, y=190)

    message_Variable = tkinter.StringVar()
    message_Entry = tkinter.Entry(root, textvariable=message_Variable, bd=4, width=28, font="Helvetica 16 italic")
    message_Entry.focus()
    message_Entry.place(x=20, y=280)

    email = email_Variable.get()
    password = password_Variable.get()
    sender = email_Variable.get()
    reciever = to_Variable.get()
    message = message_Variable.get()
    subject = subject_Variable.get()

    send_Button = tkinter.Button(root, text="Send", fg="White", bg="Green", width=8, height=2, font="Bizon 8 bold", command=lambda:testInfo(email, password, sender, reciever, message, subject))
    send_Button.place(x=300, y=355)

    #Locks window size.
    root.minsize(385,425)
    root.maxsize(385,425)

    #Changes favicon in upper-left corner of master window.
root.iconbitmap(r"C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Email Buddy\Images\EmailBuddy_Icon.ico")

    root.mainloop()

#Created a main log in window (master = Log In).
master = tkinter.Tk()
master.title("Email Buddy | Log In")

#Master text
master_text = tkinter.Label(master, text="Sign In", font="Bizon 36 bold", fg="Red")
master_text.place(x=90, y=20)

master_text2 = tkinter.Label(master, text="Email", font="Bizon 20 bold")
master_text2.place(x=20, y=100)

master_text3 = tkinter.Label(master, text="Password", font="Bizon 20 bold")
master_text3.place(x=20, y=190)

#Master entries
email_Variable = tkinter.StringVar()
email_Entry = tkinter.Entry(master, bd=4, width=25, textvariable=email_Variable, font="Helvetica 16 italic", fg="Red")
email_Entry.focus()
email_Entry.place(x=20, y=140)

password_Variable = tkinter.StringVar()
password_Entry = tkinter.Entry(master, bd=4, width=25,        textvariable=password_Variable, font="Helvetica 16 italic", fg="Red")
password_Entry.focus()
password_Entry.place(x=20, y=230)

#Sign-In Button
signIn_Button = tkinter.Button(master, text="Sign In", font="Bizon 8 bold", fg="White", bg="Green", width=8, height=2, command=lambda:root())
signIn_Button.place(x=265, y=295)

#Locks window size
master.minsize(350, 350)
master.maxsize(350, 350)

#Changes favicon in upper-left corner of master window.
master.iconbitmap(r"C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Email Buddy\Images\EmailBuddy_Icon.ico")

master.mainloop()

Screenshot:

First window

enter image description here

Second window

enter image description here

Output in console enter image description here

0

2 Answers 2

0

The crux of the problem is that you are getting the values of the variables when you first create the button, at which point the to, subject and message are blank.

The proper way to code a GUI is to get the values when you need them, and not before. This eliminates the need for lambda; lambda is rarely truly necessary and makes the code harder to write, harder to understand, and harder to modify.

Also, my advice is to not use the StringVars, and modify testInfo to fetch the data at the time it needs it. The use of StringVar is just one more object you need to keep track of, and is completely unnecessary unless you're making use of the special features of those objects (which you're not).

def testInfo():
    reciever = to_Entry.get()
    email = email_Entry.get()
    password = password_Entry.get()
    sender = email_Entry.get()
    message = message_Entry.get()
    subject = subject_Entry.get()

    print("Email : " + email)
    print("Password : " + password)
    print("From : " + sender)
    print("To : " + reciever)
    print("Message : " + message)
    print("Subject : " + subject)

...
send_Button = tkinter.Button(..., command=testInfo)
5
  • Okay i'm getting the results i've wanted, but i dont quite understand what you supplied me with. Can you explain this to me..... >>> what does lambda do? and also if i dont use stringvars() what do i use? Also why are the subjects blank before i create the button? What do you mean by that?
    – user4610150
    Sep 1, 2015 at 16:23
  • 2
    @PamalMangat: If you don't know what lambda does, that's an extra reason why you shouldn't be using it. It creates an anonymous function (a def, but without a name). If you don't use stringvars, you simply get the data directly from the entry widget itself. Sep 1, 2015 at 17:08
  • can you please do me a huge favor and explain to me how to find out if the user entered the correct email and password when logging in. If not i want to be able to lock them out of access. PLEASE HELP ME! MY ACCOUNT HAS BEEN BANNED FROM ASKING ANYMORE QUESTION :( :( :(
    – user4610150
    Sep 2, 2015 at 17:25
  • @PamalMangat: no, sorry. Stackoverflow is not a code-writing service or site for long discussions. Since you have been banned, the best option is to wait for the ban to lift, then try to improve the quality of your questions so you don't get banned again, and then ask a new question. Sep 2, 2015 at 18:44
  • Do i just wait to be unbanned or do i have to do something?
    – user4610150
    Sep 3, 2015 at 16:29
0

Just an extra tip: Instead of defining the min and max size of the root.You can simply do:

root.resizable(False, False)

The first false stands for x axis while the other for y..

2
  • Okay thanks i'll remember to do that. Also they do the same thing though so does it really matter.
    – user4610150
    Sep 1, 2015 at 16:02
  • IMO setting resizable to False is very user-unfriendly. I recommend never doing that. You don't know what fonts or resolution the end user may have. I hate applications that remove my ability to resize them. Sep 1, 2015 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.