Skip to content

Instantly share code, notes, and snippets.

@thodnev
Created August 29, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thodnev/8cd74504a776933bcf110574c7bb7c76 to your computer and use it in GitHub Desktop.
Save thodnev/8cd74504a776933bcf110574c7bb7c76 to your computer and use it in GitHub Desktop.
# TODO: complete it!
'''
Contains login-promt frame tkinter widget
'''
import tkinter as tk
from tkinter.ttk import Combobox
class LoginFrame(tk.Frame):
'''
A login-promt frame tkinter widget
'''
__strings = {'login': 'Login:',
'password': 'Password:',
'ok': 'OK',
'cancel': 'Cancel'
}
__styles = {'login_label': {'font': 'helvetica 11'},
'login_combo': {'font': 'helvetica 11'},
'password_label': {'font': 'helvetica 11'},
'password_entry': {'highlightthickness': 0,
'font': 'helvetica 11'},
'ok_button': {'font': 'helvetica 11'},
'cancel_button': {'font': 'helvetica 11'}
}
def __init__(self, *args, users, hidden_users=None, hashfunc=None, command=None,
cancel_command=None, error_command=None, text=None, **kwargs):
self.users = users
self.hidden_users = hidden_users or []
self.hashfunc = hashfunc or (lambda passw: passw)
self.command = command or (lambda user: None)
self.cancel_command = cancel_command or (lambda: None)
self.error_command = error_command or (lambda user: None)
if text is not None:
self.__strings = dict(text)
super().__init__(*args, **kwargs)
self.__make_widgets()
def __make_widgets(self):
# create subframes
login_frame = tk.Frame(self)
login_frame.columnconfigure(1, weight=100) # to make column expandable
login_frame.rowconfigure(0, weight=100)
login_frame.rowconfigure(1, weight=100)
login_frame.pack(expand=True, fill='both')
buttons_frame = tk.Frame(self)
buttons_frame.pack(expand=True, fill='both', anchor='e')
# login field
login_label = tk.Label(login_frame, text=self.__strings['login'],
**self.__styles['login_label'])
login_label.grid(row=0, column=0, sticky='W')
displayed_users = sorted(set(self.users) - set(self.hidden_users))
login_combo = Combobox(login_frame, values=displayed_users,
**self.__styles['login_combo'])
login_combo.bind('<<ComboboxSelected>>',
lambda event: login_combo.selection_clear())
login_combo.grid(row=0, column=1, columnspan=5, sticky='EW')
self.get_login = login_combo.get
# password field
password_label = tk.Label(login_frame, text=self.__strings['password'],
**self.__styles['password_label'])
password_label.grid(row=1, column=0, sticky='W')
password_entry = tk.Entry(login_frame, show='*',
**self.__styles['password_entry'])
password_entry.bind('<Return>', lambda event: self._ok_callback())
password_entry.grid(row=1, column=1, columnspan=5, sticky='EW')
self.get_password = password_entry.get
# buttons
ok_button = tk.Button(buttons_frame, text=self.__strings['ok'],
command=self._ok_callback,
**self.__styles['ok_button'])
ok_button.pack(side='left', expand=True, anchor='w')
cancel_button = tk.Button(buttons_frame, text=self.__strings['cancel'],
command=self._cancel_callback,
**self.__styles['cancel_button'])
cancel_button.pack(side='right', expand=True, anchor='e')
# add password field to self
self.password_entry = password_entry
def _ok_callback(self):
login = self.get_login()
passw = self.get_password()
hashed = self.hashfunc(passw)
if login in self.users and hashed==self.users[login]:
self.command(login)
else:
self._login_error(login)
def _cancel_callback(self):
return self.cancel_command()
def _login_error(self, login):
self.password_entry.delete(0, 'end')
return self.error_command(login)
if __name__ == '__main__':
users = dict(Baboon='greatbaboon', Monkey='iammonkey', StupidMonkey='A-A-A',
thd='name')
root = tk.Tk()
fr = LoginFrame(root, users=users, hidden_users=['Monkey'],
command=lambda usr:print('User', usr, 'logged in ...'),
error_command=lambda usr:print('Error:', usr, '...'),
cancel_command=lambda: print('Exit requested ...') or fr.quit())
fr.pack(side='bottom', expand=True, fill='both')
tk.Label(root, text='ANOTHER LABEL', font='12').pack(side='top', anchor='w')
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment