Advertisement
Guest User

automata_launcher.py

a guest
Jun 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. # creates the main window object, defines its name, and default size
  4. main = Tk()
  5. main.title('Authentication Box')
  6. main.geometry('225x150')
  7.  
  8. def clear_widget(label_text_box, label_input_box):
  9.  
  10.     # will clear out any entry boxes defined below when the user shifts
  11.     # focus to the widgets defined below
  12.     if label_text_box == main.focus_get() and label_text_box.get() == 'Enter Seed':
  13.         label_text_box.delete(0, END)
  14.     elif label_input_box == label_input_box.focus_get() and label_input_box.get() == '     ':
  15.         label_input_box.delete(0, END)
  16.  
  17. def repopulate_defaults(label_text_box, label_input_box, event):
  18.  
  19.     # will repopulate the default text previously inside the entry boxes defined below if
  20.     # the user does not put anything in while focused and changes focus to another widget
  21.     if username_box != main.focus_get() and username_box.get() == '':
  22.         username_box.insert(0, 'Enter Username')
  23.     elif password_box != main.focus_get() and password_box.get() == '':
  24.         password_box.insert(0, '     ')
  25.  
  26. def login(*event):
  27.  
  28.     # Able to be called from a key binding or a button click because of the '*event'
  29.     print ( 'Username: ' + username_box.get())
  30.     print ( 'Password: ' + password_box.get())
  31.     main.destroy()
  32.     # If I wanted I could also pass the username and password I got above to another
  33.     # function from here.
  34.  
  35.  
  36. # defines a grid 50 x 50 cells in the main window
  37. rows = 0
  38. while rows < 10:
  39.     main.rowconfigure(rows, weight=1)
  40.     main.columnconfigure(rows, weight=1)
  41.     rows += 1
  42.  
  43. password_box = Entry(main, show='*')
  44. # adds username entry widget and defines its properties
  45. username_box = Entry(main)
  46. username_box.insert(0, 'Enter Username')
  47. username_box.bind("<FocusIn>", clear_widget(username_box, password_box))
  48. username_box.bind('<FocusOut>', repopulate_defaults)
  49. username_box.grid(row=1, column=5, sticky='NS')
  50.  
  51.  
  52. # adds password entry widget and defines its properties
  53. password_box.insert(0, '     ')
  54. password_box.bind("<FocusIn>", clear_widget(username_box, password_box))
  55. password_box.bind('<FocusOut>', repopulate_defaults)
  56. password_box.bind('<Return>', login)
  57. password_box.grid(row=2, column=5, sticky='NS')
  58.  
  59.  
  60. # adds login button and defines its properties
  61. login_btn = Button(main, text='Login', command=login)
  62. login_btn.bind('<Return>', login)
  63. login_btn.grid(row=5, column=5, sticky='NESW')
  64.  
  65.  
  66. main.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement