Skip to content

Commit

Permalink
Added better docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
slightlynybbled committed Feb 5, 2018
1 parent a905554 commit 31d5f20
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
8 changes: 8 additions & 0 deletions docs/widget_groups.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ The screenshot consists of three individual examples of ``KeyValueEntry`` widget

.. autoclass:: groups.KeyValueEntry
:members:

``Calendar``
-----------------

.. image:: img/calendar.png

.. autoclass:: tk_tools.Calendar
:members:
45 changes: 29 additions & 16 deletions tk_tools/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def add_row(self, data: list = None):


class KeyValueEntry(tk.Frame):
"""
r"""
Creates a key-value input/output frame.
:param parent: the parent frame
Expand Down Expand Up @@ -577,28 +577,30 @@ def _get_calendar(locale, fwday):


class Calendar(ttk.Frame):
"""
r"""
Graphical date selection widget, with callbacks.
:param parent: the parent frame
:param callback: the callable to be executed on selection
:param kw: tkinter.frame keyword arguments
"""
timedelta = datetime.timedelta
datetime = datetime.datetime

def __init__(self, parent, callback=None, **kw):
def __init__(self, parent, callback=None, **kwargs):
# remove custom options from kw before initializing ttk.Frame
fwday = calendar.SUNDAY
year = kw.pop('year', self.datetime.now().year)
month = kw.pop('month', self.datetime.now().month)
locale = kw.pop('locale', None)
sel_bg = kw.pop('selectbackground', '#ecffc4')
sel_fg = kw.pop('selectforeground', '#05640e')
year = kwargs.pop('year', self.datetime.now().year)
month = kwargs.pop('month', self.datetime.now().month)
locale = kwargs.pop('locale', None)
sel_bg = kwargs.pop('selectbackground', '#ecffc4')
sel_fg = kwargs.pop('selectforeground', '#05640e')

self._date = self.datetime(year, month, 1)
self._selection = None # no date selected
self.callback = callback

super().__init__(parent, **kw)
super().__init__(parent, **kwargs)

self._cal = _get_calendar(locale, fwday)

Expand Down Expand Up @@ -721,7 +723,9 @@ def _build_calendar(self):
self._calendar.item(item, values=fmt_week)

def _show_selection(self, text, bbox):
"""Configure canvas for a new selection."""
r"""
Configure canvas for a new selection.
"""
x, y, width, height = bbox

textw = self._font.measure(text)
Expand All @@ -735,7 +739,9 @@ def _show_selection(self, text, bbox):
# Callbacks

def _pressed(self, evt):
"""Clicked somewhere in the calendar."""
r"""
Clicked somewhere in the calendar.
"""
x, y, widget = evt.x, evt.y, evt.widget
item = widget.identify_row(y)
column = widget.identify_column(x)
Expand Down Expand Up @@ -765,23 +771,28 @@ def _pressed(self, evt):
self.callback()

def add_callback(self, callback: callable):
"""
r"""
Adds a callback to call when the user clicks on a date
:param callback: a callable function
:return: None
"""
self.callback = callback

def _prev_month(self):
"""Updated calendar to show the previous month."""
r"""
Updated calendar to show the previous month.
"""
self._canvas.place_forget()

self._date = self._date - self.timedelta(days=1)
self._date = self.datetime(self._date.year, self._date.month, 1)
self._build_calendar() # reconstruct calendar

def _next_month(self):
"""Update calendar to show the next month."""
r"""
Update calendar to show the next month.
"""
self._canvas.place_forget()

year, month = self._date.year, self._date.month
Expand All @@ -792,7 +803,9 @@ def _next_month(self):

@property
def selection(self):
"""Return a datetime representing the current selected date."""
r"""
Return a datetime representing the current selected date.
"""
if not self._selection:
return None

Expand Down

0 comments on commit 31d5f20

Please sign in to comment.