[Tutor] login window using Tk

Alan Gauld alan.gauld at btinternet.com
Tue Nov 1 21:49:14 CET 2011


On 01/11/11 18:57, Chris Hare wrote:
> Here is a code snippet I have pulled out of the project. It is as bare
> bones as I can make it to get the point across.

I think you could have dropped a lot more to be honst - like all the 
menu code for a start...

> 1. I would really like the window to be centered in the user's screen,
> but setting the geometry doesn't place it there. (that isn't included here)

OK, But thats a problem for another day.
And some of these things are OS/UI dependent so you need to tell us what 
OS you are on.


> 2. When I click the Login button, nothing happens. I know I am missing
> something but it just isn't obvious what it is.

You don't provide a command in the command parameter. You provide a 
string. When you click the button Python will try to call the string, 
which won't work. You should get an error message in the console -= how 
are you running this? Is it from a command prompt? If not you may not be 
seeing all the error messages Python is sending you...

> 3. Finally, I would like to be able to hide the root window until the
> authentication is performed, but root.hide() gets me a getattr error.
> root.withdraw() works, but I can't get the root window back

Really? You want the Login window to disappear even before you have 
authenticated the user? Thats very unusual behaviour. Usually the login 
window only goes away once you have been authenticated.


> import sys
> from Tkinter import *
> import tkMessageBox
> import tkFont
>
> class Login:
> def __init__(self,parent):
> self.window = parent
>
> def show(instance):
> window = Toplevel()
> frame = Frame(window,bg=backColor)
 > ...
> label2 = Label(frame, text = "Username", fg="Blue", bg=backColor)
> label3 = Label(frame, text = "Password", fg="Blue", bg=backColor)
> login_userid = Entry(frame,bg=outFocusColor)
> login_passwd = Entry(frame,bg=outFocusColor,show="*")
> login_userid.bind("<Return>", login_passwd.focus_set())
> btnLogin = Button(frame, text="Login", command="print button
> pressed",highlightbackground=backColor)

you need to provide the name of a function to the command parameter - or 
use a lambda expression if its just a one liner

> frame.grid()
> label2.grid(row=2, column=0,columnspan=3, sticky=W)
> login_userid.grid(row=2, column=6, columnspan=5,sticky=W)
> label3.grid(row=3, column=0,columnspan=3, sticky=W)
> login_passwd.grid(row=3, column=6, columnspan=5,sticky=W)
> btnLogin.grid(row=4, column=4, sticky=W)


> if __name__ == "__main__":
> root = Tk()
> root.withdraw()
> l = Login(root)
> l.show()
>
> root.mainloop()

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list