[Tutor] login window using Tk

Chris Hare chare at labr.net
Tue Nov 1 19:57:14 CET 2011


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.

the problems I am having:

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)
2.  When I click the Login button, nothing happens.  I know I am missing something but it just isn't obvious what it is.
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 

Thanks for your help.  

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)
		menubar = Menu(window)
		filemenu = Menu(menubar, tearoff=0)
		filemenu.add_command(label="Exit", command="sys.exit()")
		menubar.add_cascade(label="File", menu=filemenu)
		window.config(menu=menubar)
		programName = Label(frame, text = "test")
		list = Listbox(frame)
		list.insert( END,"test")
		label1 = Label(frame, text = "Organization", fg="Blue", 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)

		frame.title = "Login to application" 
		list.focus_set()
		frame.grid()
		programName.grid(row=0, column=0,columnspan=5,sticky=W)
		label1.grid(row=1, column=0,columnspan=3, sticky=W)
		list.grid(row=1, column=6, columnspan=5, sticky=W)
		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__":
	backColor = "Gray"
	entryColor = "Cyan"
	okColor = "Green"
	warnColor = "Red"
	inFocusColor = "Cyan"
	outFocusColor = "White"

	root = Tk()
	root.withdraw()
	l = Login(root)
	l.show()

	root.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111101/7dcd397f/attachment-0001.html>


More information about the Tutor mailing list