tools.helpers.interface package

Submodules

tools.helpers.interface.advanced_wrappers module

Add advanced dialogs in the wrapped messagebox, simpledialog and filedialog module-like objects. Import messagebox, simpledialog and filedialog from this file instead from tkinter to benefit from enhanced modules.

tools.helpers.interface.anomalies module

Functions to display errors in message boxes and dialog with the user.

tools.helpers.interface.anomalies.raise_anomaly(flag='ask', error=None, title=None, message=None, use_messagebox=True)
Raise an ‘anomaly’ depending on the flag argument:
  • ‘ignore’: do nothing and return,

  • ‘warning’: show a warning (warning logger and tkinter messagebox),

  • ‘error’: raises an exception,

  • ‘ask’: ask the user either to ignore the anomaly or to raise an error.

Parameters
  • flag – must be ‘ask’, ‘ignore’, ‘warning’, ‘error’

  • error – Exception

  • title – Title for message box and logger.

  • message – Message for message box and error logger.

  • use_messagebox – if True the tkinter messagebox will be used to show errors.

Returns

tools.helpers.interface.anomalies.raise_bad_extension_anomaly(flag, use_messagebox=True, msg=None)

Anomaly if bad extension set.

Parameters
  • flag – must be ‘ask’, ‘ignore’, ‘warning’, or ‘error’

  • use_messagebox – boolean. if True a messagebox will appear

  • msg – custom message

Returns

tools.helpers.interface.anomalies.raise_no_file_selected_anomaly(flag, use_messagebox=True)

Anomaly if no file selected.

Parameters
  • flag – must be ‘ask’, ‘ignore’, ‘warning’, or ‘error’

  • use_messagebox – boolean. if True a messagebox will appear

Returns

tools.helpers.interface.basics module

Model classes and basic functions.

class tools.helpers.interface.basics.CustomTk(title=None, **options)

Bases: tkinter.Tk

class tools.helpers.interface.basics.FrameWithScrollbar(master=None, **options)

Bases: tkinter.ttk.Frame

Frame with a vertical scrollbar. Access to canvas: ‘.canvas’; access to child frame: ‘.frame’

on_frame_configure(_event)

Reset the scroll region to encompass the inner frame

tools.helpers.interface.basics.center(win, width=None, height=None)

Centers a tkinter window. :param win: the root or Toplevel window to center :param width: custom width for center computation :param height: custom height for center computation

tools.helpers.interface.custom_dialog module

Defines advanced dialogs to select options within multiple choices. Based on the following tkinter.ttk widgets: Checkbutton, Radiobutton, OptionMenu.

class tools.helpers.interface.custom_dialog.CheckBoxFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_status=False, nb_columns=10, **options)

Bases: tkinter.ttk.Frame

Frame with checkboxes.

all_command()
check()
none_command()
property result

Returns results

property result_keys

Returns the key/index value of the result in choices

validate()

Returns whether the entry is correct or not: for check box, the answer is always True.

class tools.helpers.interface.custom_dialog.CheckBoxSelectorDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.custom_dialog.CheckBoxSelectorDialog

Dialog window with checkboxes.

body(master, **kwargs)

Widget body

class tools.helpers.interface.custom_dialog.MultiSelectorDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.custom_dialog.MultiSelectorDialog

Window which looks like a messagebox window, but with custom choices (list of tuples).

body(master, **kwargs)

Widget body

class tools.helpers.interface.custom_dialog.MultiSelectorFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_status=False, initial_value=None, nb_columns=10, date_start=None, date_end=None, dateformat=None, default_box_type='check_box', max_width=1500, max_height=500, **options)

Bases: tkinter.ttk.Frame

Frame with multiple checkboxes, radio buttons or option menu.

property result

Returns the results

property result_keys

Returns the key/index value of the result in choices

validate()
class tools.helpers.interface.custom_dialog.OptionMenuDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.custom_dialog.OptionMenuDialog

Dialog window with an OptionMenu widget.

body(master, **kwargs)

Widget body

class tools.helpers.interface.custom_dialog.OptionMenuFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_value=None, **options)

Bases: tkinter.ttk.Frame

Frame with an OptionMenu widget.

property result

Returns the result

property result_keys

Returns the result

validate()

Returns whether the entry is correct or not, i.e. if it is in choices

class tools.helpers.interface.custom_dialog.RadioButtonFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_value=None, nb_columns=10, **options)

Bases: tkinter.ttk.Frame

Frame with radio buttons.

property result

Returns result

property result_keys

Returns the key/index value of the result in choices

validate()

Returns whether the entry is correct or not, i.e. if the answer value is in choices values

class tools.helpers.interface.custom_dialog.RadioSelectorDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.custom_dialog.RadioSelectorDialog

Dialog window with radio buttons.

body(master, **kwargs)

Widget body

tools.helpers.interface.custom_dialog.ask_checkbox(title: str = None, message: str = None, choices: Union[list, dict] = None, initial_status: bool = None, **options)
tools.helpers.interface.custom_dialog.ask_multiple_questions(title: str = None, message: str = None, choices: Union[list, dict] = None, initial_status: bool = False, **options)
tools.helpers.interface.custom_dialog.ask_option(title: str = None, message: str = None, choices: Union[list, dict] = None, initial_value: bool = None, **options)
tools.helpers.interface.custom_dialog.ask_radio_button(title: str = None, message: str = None, choices: Union[list, dict] = None, initial_value: bool = None, **options)

tools.helpers.interface.custom_messagebox module

Defines advanced messagebox to select options within multiple choices.

class tools.helpers.interface.custom_messagebox.CustomQuestionDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.custom_messagebox.CustomQuestionDialog

Window which looks like a messagebox window, but with custom answers (list of tuples).

body(master, **kwargs)

Widget body

class tools.helpers.interface.custom_messagebox.CustomQuestionFrame(master=None, message: str = None, choices: Union[list, dict] = None, auto_quit=True, geometry_manager='pack', geometry_options=None, **options)

Bases: tkinter.ttk.Frame

Frame with custom answers displayed as tkinter buttons.

return_answer(key)
validate()
tools.helpers.interface.custom_messagebox.ask_custom_question(title: str = None, message: str = None, choices: Union[list, dict] = None, **options)

tools.helpers.interface.date_picker module

Defines a dialog to select a date with a calendar.

class tools.helpers.interface.date_picker.Calendar(master=None, year=None, month=None, firstweekday=0, locale=None, activebackground='#b1dcfb', activeforeground='black', selectbackground='#003eff', selectforeground='white', command=None, borderwidth=1, relief='solid', on_click_month_button=None)

Bases: tkinter.ttk.Frame

class datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

Bases: datetime.date

The year, month and day arguments are required. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be ints.

astimezone()

tz -> convert to local time in new timezone tz

combine()

date, time -> datetime with same date and time fields

ctime()

Return ctime() style string.

date()

Return date object with same year, month and day.

dst()

Return self.tzinfo.dst(self).

fromtimestamp()

timestamp[, tz] -> tz’s local time from POSIX timestamp.

hour
isoformat()

[sep] -> string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].

sep is used to separate the year from the time, and defaults to ‘T’.

max = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
microsecond
min = datetime.datetime(1, 1, 1, 0, 0)
minute
now()

Returns new datetime object representing current time local to tz.

tz

Timezone object.

If no tz is specified, uses local timezone.

replace()

Return datetime with new specified fields.

resolution = datetime.timedelta(0, 0, 1)
second
strptime()

string, format -> new datetime parsed from a string (like time.strptime()).

time()

Return time object with same time but with tzinfo=None.

timestamp()

Return POSIX timestamp as float.

timetuple()

Return time tuple, compatible with time.localtime().

timetz()

Return time object with same time and tzinfo.

tzinfo
tzname()

Return self.tzinfo.tzname(self).

utcfromtimestamp()

Construct a naive UTC datetime from a POSIX timestamp.

utcnow()

Return a new datetime representing UTC day and time.

utcoffset()

Return self.tzinfo.utcoffset(self).

utctimetuple()

Return UTC time tuple, compatible with time.localtime().

get_selection()

Return a datetime representing the current selected date.

next_month()

Update calendar to show the next month.

next_year()

Update calendar to show the next year.

prev_month()

Updated calendar to show the previous week.

prev_year()

Updated calendar to show the previous year.

select_current_date()

Update calendar to current date.

select_next_day()

Update calendar to show the next day.

select_next_week_day()

Update calendar to show the next week.

select_prev_day()

Updated calendar to show the previous day.

select_prev_week_day()

Updated calendar to show the previous week.

selection()

Return a datetime representing the current selected date.

set_selection(date)

Set the selected date.

class timedelta

Bases: object

Difference between two datetime values.

days

Number of days.

max = datetime.timedelta(999999999, 86399, 999999)
microseconds

Number of microseconds (>= 0 and less than 1 second).

min = datetime.timedelta(-999999999)
resolution = datetime.timedelta(0, 0, 1)
seconds

Number of seconds (>= 0 and less than 1 day).

total_seconds()

Total seconds in the duration.

class tools.helpers.interface.date_picker.DatePickerDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.date_picker.DatePickerDialog

Dialog window with date picker.

body(master, **kwargs)

Widget body

class tools.helpers.interface.date_picker.DatePickerFrame(master=None, message: str = None, initial_value=None, dateformat=None, **options)

Bases: tkinter.ttk.Frame

Integration of DatePicker in a Frame

property result

Returns result: current date

property result_keys

Returns the text value of the result

validate()

Returns whether the entry is correct or not: for check box, the answer is always True.

class tools.helpers.interface.date_picker.DatePickerTk(title=None, message=None, default_date=None, dateformat='%Y-%m-%d')

Bases: tools.helpers.interface.date_picker.MainDateTk

class tools.helpers.interface.date_picker.DateSelectorDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.date_picker.DateSelectorDialog

Dialog window with two date pickers to select dates inside a time period.

body(master, **kwargs)

Widget body

class tools.helpers.interface.date_picker.DateSelectorFrame(master=None, message=None, mode='auto', choices=None, date_start=None, date_end=None, dateformat=None, allow_empty=True, **options)

Bases: tkinter.ttk.Frame

Two date pickers selecting dates from a start date (included) to an end date (excluded)

If a date is empty, the considered value is None and dates are not filtered.

property filtered_result

Returns the dates in choices filtered

property result

Returns result: filtered dates or tuple of selected dates

property result_keys

Returns the two dates defining the interval

validate()

Returns whether the entry is correct or not: for check box, the answer is always True.

class tools.helpers.interface.date_picker.Datepicker(master, entrywidth=None, entrystyle=None, datevar=None, dateformat='%Y-%m-%d', onselect=None, firstweekday=0, locale=None, activebackground='#b1dcfb', activeforeground='black', selectbackground='#003eff', selectforeground='white', borderwidth=1, relief='solid')

Bases: tkinter.ttk.Entry

property current_date
property current_text
erase()
hide_calendar()
property is_calendar_visible
property is_valid_date
set_date_from_calendar()
show_calendar()
show_date_on_calendar()
class tools.helpers.interface.date_picker.MainDateTk

Bases: tools.helpers.interface.basics.CustomTk

close()
on_closing()
tools.helpers.interface.date_picker.askdate(title: str = None, message: str = None, initial_value: datetime.datetime = None, **options)

Ask a date to the user.

Parameters
  • title – window title

  • message – message to show before the entry of dialog

  • initial_value – first date to show in the entry of dialog. If None the current date is used.

  • bypass_dialog – if True, no dialog is opened and the function is executed, returning either: * initial_value if initial_value is as date (datetime.datetime type) * the current date if initial_value is None * None in the other cases

  • options – options for the dialog class

Returns

datetime.datetime object or None

tools.helpers.interface.date_picker.askperiod(title: str = None, message: str = None, mode: str = 'auto', choices: Union[list, dict] = None, date_start: datetime.datetime = None, date_end: datetime.datetime = None, dateformat: str = None, **options)
tools.helpers.interface.date_picker.get_user_date(title=None, message=None, default_date=None, dateformat='%Y-%m-%d', bypass_dialog=False, behavior_on_error='ignore')

Main function to get a date from user.

tools.helpers.interface.logger_widget module

Defines a text frame updated by a logger handler. Support multi-threading.

class tools.helpers.interface.logger_widget.LoggingHandlerFrame(master=None, level=20, height=200, width=500, **options)

Bases: tools.helpers.interface.text_frame.TextFrame

Text frame updated by a logger handler.

class Handler(widget, level=20)

Bases: logging.Handler

emit(record)

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

quit()

Detach logger before closing

tools.helpers.interface.main_interface module

Interface with multiple parts containing buttons corresponding to one function each. Includes a logger frame and a help window.

class tools.helpers.interface.main_interface.MainTk(parts: collections.OrderedDict, logger_level=20, nb_columns=3, help_msg='No help_msg available.', title='main', **options)

Bases: tools.helpers.interface.basics.CustomTk

Main Tk window

activate_all()
static ask_exit(name='unknown')
disable_all()
load_help()
on_closing()
open_help(_event=None)

tools.helpers.interface.text_frame module

Frame, dialog and Tk window with text in it.

class tools.helpers.interface.text_frame.TextDialog(parent=None, title=None, **kwargs)

Bases: tools.helpers.interface.text_frame.TextDialog

Text frame integrated in a dialog

body(master, **kwargs)

Widget body

class tools.helpers.interface.text_frame.TextFrame(master=None, text='', height=600, width=500, **options)

Bases: tkinter.ttk.Frame

Frame with text in it.

class tools.helpers.interface.text_frame.TextTk(title=None, text='', **options)

Bases: tools.helpers.interface.basics.CustomTk

Text window

tools.helpers.interface.text_frame.showtext(title: str = None, text: str = None, **options)

Show a long text in a dialog

tools.helpers.interface.tooltip module

Create a tooltip for a given widget.

class tools.helpers.interface.tooltip.ToolTip(widget, text='widget info')

Bases: object

Create a tooltip for a given widget

enter(_event=None)
hidetip()
leave(_event=None)
schedule()
showtip(_event=None)
unschedule()

tools.helpers.interface.wrappers module

Modified messagebox, simpledialog and filedialog (tkinter modules) to avoid empty Tk windows that cannot be closed before the end of the program.

class tools.helpers.interface.wrappers.CustomDialog(parent=None, title=None, **kwargs)

Bases: tkinter.Toplevel

Class to open custom dialogs. Adapted from tkinter.simpledialog.Dialog. See its doc for more info.

apply()
body(master, **kwargs)
cancel(_event=None)
destroy()

Destroy this and all descendants widgets.

ok(_event=None)
on_closing()
validate()
tools.helpers.interface.wrappers.convert_to_dialog(dialog_cls, func=<class 'object'>)

Returns a function that open a dialog window and returns its result.

tools.helpers.interface.wrappers.dialog_function(dialog_cls)

Decorator wrapper to create a function that open a dialog window and returns its result.

The function is completely overwritten, except its signature and docstring (see functools.wraps), except if the keyword argument ‘bypass_dialog’ is set to True. In this case, the original function is executed.

tools.helpers.interface.wrappers.frame_integration(frame: tkinter.ttk.Frame, okcancel=False)

Decorator wrapper to create a Tk window class from a Frame class.

>>> class MyFrame(ttk.Frame): pass
>>> class MyDialog(CustomDialog): pass
>>> FrameIntegratedInDialog = frame_integration(MyFrame)(MyDialog)
tools.helpers.interface.wrappers.integrate_frame_in_dialog(frame, okcancel=False, base_dialog=<class 'tools.helpers.interface.wrappers.CustomDialog'>)

Returns a Dialog class which contains an integrated frame.

Module contents

This module provides classes and functions based on tkinter module.

Files:
  • basics: basic classes and functions

  • wrappers and advanced_wrappers: replace and enhance tkinter filedialog, simpledialog and messagebox modules

  • custom_messagebox: add a new method to messagebox to allow customized answers in a dialog window

  • custom_dialog: add new methods to simpledialog with check boxes, radio buttons, option menus or a mix.

  • date_picker: interface with a calendar to select a date

  • text_frame: display text in a frame

  • logger_widget: handler and frame to show logging outputs in tkinter windows

  • tooltip: easily add a tooltip on a widget

  • anomalies: show error messages on errors

  • main_interface: simple interface with multiple parts. Each button correspond to a function

class tools.helpers.interface.CustomTk(title=None, **options)

Bases: tkinter.Tk

tools.helpers.interface.center(win, width=None, height=None)

Centers a tkinter window. :param win: the root or Toplevel window to center :param width: custom width for center computation :param height: custom height for center computation

tools.helpers.interface.get_user_date(title=None, message=None, default_date=None, dateformat='%Y-%m-%d', bypass_dialog=False, behavior_on_error='ignore')

Main function to get a date from user.

class tools.helpers.interface.DatePickerFrame(master=None, message: str = None, initial_value=None, dateformat=None, **options)

Bases: tkinter.ttk.Frame

Integration of DatePicker in a Frame

property result

Returns result: current date

property result_keys

Returns the text value of the result

validate()

Returns whether the entry is correct or not: for check box, the answer is always True.

class tools.helpers.interface.DateSelectorFrame(master=None, message=None, mode='auto', choices=None, date_start=None, date_end=None, dateformat=None, allow_empty=True, **options)

Bases: tkinter.ttk.Frame

Two date pickers selecting dates from a start date (included) to an end date (excluded)

If a date is empty, the considered value is None and dates are not filtered.

property filtered_result

Returns the dates in choices filtered

property result

Returns result: filtered dates or tuple of selected dates

property result_keys

Returns the two dates defining the interval

validate()

Returns whether the entry is correct or not: for check box, the answer is always True.

class tools.helpers.interface.OptionMenuFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_value=None, **options)

Bases: tkinter.ttk.Frame

Frame with an OptionMenu widget.

property result

Returns the result

property result_keys

Returns the result

validate()

Returns whether the entry is correct or not, i.e. if it is in choices

class tools.helpers.interface.RadioButtonFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_value=None, nb_columns=10, **options)

Bases: tkinter.ttk.Frame

Frame with radio buttons.

property result

Returns result

property result_keys

Returns the key/index value of the result in choices

validate()

Returns whether the entry is correct or not, i.e. if the answer value is in choices values

class tools.helpers.interface.CheckBoxFrame(master=None, message: str = None, choices: Union[list, dict] = None, initial_status=False, nb_columns=10, **options)

Bases: tkinter.ttk.Frame

Frame with checkboxes.

all_command()
check()
none_command()
property result

Returns results

property result_keys

Returns the key/index value of the result in choices

validate()

Returns whether the entry is correct or not: for check box, the answer is always True.

class tools.helpers.interface.CustomQuestionFrame(master=None, message: str = None, choices: Union[list, dict] = None, auto_quit=True, geometry_manager='pack', geometry_options=None, **options)

Bases: tkinter.ttk.Frame

Frame with custom answers displayed as tkinter buttons.

return_answer(key)
validate()
class tools.helpers.interface.TextFrame(master=None, text='', height=600, width=500, **options)

Bases: tkinter.ttk.Frame

Frame with text in it.

tools.helpers.interface.raise_anomaly(flag='ask', error=None, title=None, message=None, use_messagebox=True)
Raise an ‘anomaly’ depending on the flag argument:
  • ‘ignore’: do nothing and return,

  • ‘warning’: show a warning (warning logger and tkinter messagebox),

  • ‘error’: raises an exception,

  • ‘ask’: ask the user either to ignore the anomaly or to raise an error.

Parameters
  • flag – must be ‘ask’, ‘ignore’, ‘warning’, ‘error’

  • error – Exception

  • title – Title for message box and logger.

  • message – Message for message box and error logger.

  • use_messagebox – if True the tkinter messagebox will be used to show errors.

Returns

tools.helpers.interface.raise_bad_extension_anomaly(flag, use_messagebox=True, msg=None)

Anomaly if bad extension set.

Parameters
  • flag – must be ‘ask’, ‘ignore’, ‘warning’, or ‘error’

  • use_messagebox – boolean. if True a messagebox will appear

  • msg – custom message

Returns

tools.helpers.interface.raise_no_file_selected_anomaly(flag, use_messagebox=True)

Anomaly if no file selected.

Parameters
  • flag – must be ‘ask’, ‘ignore’, ‘warning’, or ‘error’

  • use_messagebox – boolean. if True a messagebox will appear

Returns

class tools.helpers.interface.ToolTip(widget, text='widget info')

Bases: object

Create a tooltip for a given widget

enter(_event=None)
hidetip()
leave(_event=None)
schedule()
showtip(_event=None)
unschedule()
class tools.helpers.interface.LoggingHandlerFrame(master=None, level=20, height=200, width=500, **options)

Bases: tools.helpers.interface.text_frame.TextFrame

Text frame updated by a logger handler.

class Handler(widget, level=20)

Bases: logging.Handler

emit(record)

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

quit()

Detach logger before closing

class tools.helpers.interface.MainTk(parts: collections.OrderedDict, logger_level=20, nb_columns=3, help_msg='No help_msg available.', title='main', **options)

Bases: tools.helpers.interface.basics.CustomTk

Main Tk window

activate_all()
static ask_exit(name='unknown')
disable_all()
load_help()
on_closing()
open_help(_event=None)