0

I'm new to Python and I have a simple Tkinter program that logs you into your Gmail account and checks with the server to authenticate your login it looks like this

from EmailServer import CheckServer

from Tkinter import *
import smtplib
win = Tk()
usr = Label(win, text="Username")
psw = Label(win, text="Password",)
username = Entry(win)
password = Entry(win, show="*")
def Login():
    username1 = username.get()
    password1 = password.get()
    CheckServer(username1, password1)

b = Button(win, text="Login",command=Login)
quit = Button(win, text="Exit",command=exit)
quit.grid(row=2, column=1)
username.grid(row=0, column=1)
password.grid(row=1, column=1)
psw.grid(row=1, column=0)
usr.grid(row=0, column=0)
b.grid(row=2, column=0)

And it gets the CheckServer function from here

def CheckServer(username,password):
    import smtplib
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    print "checked"

but when I run it everything shows up but when i type everything in and press the login button it freezes up and stops responding I have no idea whats causing the the problem, any ideas?

nbro
  • 15,395
  • 32
  • 113
  • 196
  • 1
    tkinter is (unfortunately) single threaded so the gui can only update itself when the function finishes running, so if it blocks (never finishes) because it's running a server indefinitely then your gui stops responding since it never resumes it's mainloop – Tadhg McDonald-Jensen Mar 14 '17 at 00:36
  • @TadhgMcDonald-Jensen how do i fix this or is it impossible – Samuel Francis Mar 14 '17 at 00:46
  • Who said it's impossible? I didn't say that! ;) It means that `CheckServer` is either getting stuck or isn't just sending a single connection but blocking indefinitely in which case it probably isn't the correct function, is there any documentation for it? (assuming you didn't write it) – Tadhg McDonald-Jensen Mar 14 '17 at 00:47
  • Just don't run the server on the GUI thread. Spawn a new thread (or use some abstraction), and have the server run on that. – Carcigenicate Mar 14 '17 at 00:54
  • @TadhgMcDonald-Jensen the CheckSever is my code but uses python's STMP library in it – Samuel Francis Mar 14 '17 at 01:01
  • hmm, looks like you are facing [this issue](http://stackoverflow.com/questions/11981907/python-when-sending-email-always-blocked-in-the-clause-smtpserver-smtplib-s). Might get some relevant help from there. – Tadhg McDonald-Jensen Mar 14 '17 at 01:07
  • @TadhgMcDonald-Jensen tried that still with no success – Samuel Francis Mar 14 '17 at 01:13
  • Please do a little research before asking a question. This question has been asked many times on this site. – Bryan Oakley Mar 14 '17 at 01:13
  • @BryanOakley thats why im here i looked into this for 45 minutes – Samuel Francis Mar 14 '17 at 01:14
  • Bryan is refering to [How do you run your own code alongside Tkinter's event loop?](http://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) but using `after` only works if you have a callback to call occasionally and using threading makes communicating with the gui code in the other thread (safely) annoying, assuming it even lets go of the GIL which - according to the other question I linked to - may not happen. – Tadhg McDonald-Jensen Mar 14 '17 at 15:37
  • I think this issue is just the cross between two problems which the simple work arounds for both are not compatible with each other, I think the best solution without a whole lot of complicated code would be to set a max timeout and warn the user the program might stop responding for up to that amount of time. – Tadhg McDonald-Jensen Mar 14 '17 at 15:43

0 Answers0