Module ui.widgets.employee_picker
Employee picker for Tkinter
Expand source code
"""
Employee picker for Tkinter
"""
from tkinter.ttk import OptionMenu
def _format_employees(employees):
employees.sort(key=lambda x: x.last_name)
choices = {}
for employee in employees:
choices[_employee_as_key(employee)] = employee.id
return choices
def _employee_as_key(employee):
return f"{employee.first_name} {employee.last_name} ({employee.id})"
class EmployeePicker(OptionMenu): # pylint: disable=too-many-ancestors
"""
Employee Picker that uses an OptionMenu
"""
def __init__(self, master, variable, employees, default=None, **kwargs):
self.choices = _format_employees(employees)
self.variable = variable
if not default:
default = _employee_as_key(employees[0])
super().__init__(master, variable, default, *self.choices, **kwargs)
def get_value(self):
"""
Gets value from selected item
:return: value from selected item
"""
return self.choices[self.variable.get()]
Classes
class EmployeePicker (master, variable, employees, default=None, **kwargs)
-
Employee Picker that uses an OptionMenu
Construct a themed OptionMenu widget with master as the parent, the resource textvariable set to variable, the initially selected value specified by the default parameter, the menu values given by *values and additional keywords.
WIDGET-SPECIFIC OPTIONS
style: stylename Menubutton style. direction: 'above', 'below', 'left', 'right', or 'flush' Menubutton direction. command: callback A callback that will be invoked after selecting an item.
Expand source code
class EmployeePicker(OptionMenu): # pylint: disable=too-many-ancestors """ Employee Picker that uses an OptionMenu """ def __init__(self, master, variable, employees, default=None, **kwargs): self.choices = _format_employees(employees) self.variable = variable if not default: default = _employee_as_key(employees[0]) super().__init__(master, variable, default, *self.choices, **kwargs) def get_value(self): """ Gets value from selected item :return: value from selected item """ return self.choices[self.variable.get()]
Ancestors
- tkinter.ttk.OptionMenu
- tkinter.ttk.Menubutton
- tkinter.ttk.Widget
- tkinter.Widget
- tkinter.BaseWidget
- tkinter.Misc
- tkinter.Pack
- tkinter.Place
- tkinter.Grid
Methods
def get_value(self)
-
Gets value from selected item :return: value from selected item
Expand source code
def get_value(self): """ Gets value from selected item :return: value from selected item """ return self.choices[self.variable.get()]