Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- # creates the main window object, defines its name, and default size
- main = Tk()
- main.title('Authentication Box')
- main.geometry('225x150')
- def clear_widget(label_text_box, label_input_box):
- # will clear out any entry boxes defined below when the user shifts
- # focus to the widgets defined below
- if label_text_box == main.focus_get() and label_text_box.get() == 'Enter Seed':
- label_text_box.delete(0, END)
- elif label_input_box == label_input_box.focus_get() and label_input_box.get() == ' ':
- label_input_box.delete(0, END)
- def repopulate_defaults(label_text_box, label_input_box, event):
- # will repopulate the default text previously inside the entry boxes defined below if
- # the user does not put anything in while focused and changes focus to another widget
- if username_box != main.focus_get() and username_box.get() == '':
- username_box.insert(0, 'Enter Username')
- elif password_box != main.focus_get() and password_box.get() == '':
- password_box.insert(0, ' ')
- def login(*event):
- # Able to be called from a key binding or a button click because of the '*event'
- print ( 'Username: ' + username_box.get())
- print ( 'Password: ' + password_box.get())
- main.destroy()
- # If I wanted I could also pass the username and password I got above to another
- # function from here.
- # defines a grid 50 x 50 cells in the main window
- rows = 0
- while rows < 10:
- main.rowconfigure(rows, weight=1)
- main.columnconfigure(rows, weight=1)
- rows += 1
- password_box = Entry(main, show='*')
- # adds username entry widget and defines its properties
- username_box = Entry(main)
- username_box.insert(0, 'Enter Username')
- username_box.bind("<FocusIn>", clear_widget(username_box, password_box))
- username_box.bind('<FocusOut>', repopulate_defaults)
- username_box.grid(row=1, column=5, sticky='NS')
- # adds password entry widget and defines its properties
- password_box.insert(0, ' ')
- password_box.bind("<FocusIn>", clear_widget(username_box, password_box))
- password_box.bind('<FocusOut>', repopulate_defaults)
- password_box.bind('<Return>', login)
- password_box.grid(row=2, column=5, sticky='NS')
- # adds login button and defines its properties
- login_btn = Button(main, text='Login', command=login)
- login_btn.bind('<Return>', login)
- login_btn.grid(row=5, column=5, sticky='NESW')
- main.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement