tkcalendar¶
tkcalendar is a python module that provides the Calendar and DateEntry widgets for Tkinter. The DateEntry widget is similar to a Combobox, but the drop-down is not a list but a Calendar to select a date. Events can be displayed in the Calendar with custom colors and a tooltip displays the event list for a given day. tkcalendar is compatible with both Python 2 and Python 3. It supports many locale settings (e.g. ‘fr_FR’, ‘en_US’, ..) and the colors are customizable.
Project page: https://github.com/j4321/tkcalendar
Installation¶
Requirements
- Linux, Windows, OSX
- Python 2 or 3 with tkinter + ttk (default for Windows but not for Linux) and babel
Install
Ubuntu: use the PPA ppa:j-4321-i/ppa
$ sudo add-apt-repository ppa:j-4321-i/ppa $ sudo apt-get update $ sudo apt-get install python(3)-tkcalendar
Archlinux:
The package is available on AUR
With pip:
$ pip install tkcalendar
Documentation¶
Note
This documentation only addresses the methods specific to the Calendar
and DateEntry
widgets, but these widgets also possess the methods common to
all tkinter widgets.
and the one common to all ttk widgets.
In addition, the DateEntry
also have all the methods of the Entry widget.
Calendar¶
Class¶
-
class
tkcalendar.
Calendar
(master=None, **kw)[source]¶ Bases:
tkinter.ttk.Frame
Calendar widget.
-
__init__
(master=None, **kw)[source]¶ Construct a
Calendar
with parent master.Standard Options
- cursor : str
- cursor to display when the pointer is in the widget
- font : str or Tkinter Font instance
- font of the calendar
- borderwidth : int
- width of the border around the calendar
- state : str
- “normal” or “disabled” (unresponsive widget)
Widget-specific Options
- year : int
- intinitially displayed year, default is current year.
- month : int
- initially displayed month, default is current month.
- day : int
- initially selected day, if month or year is given but not day, no initial selection, otherwise, default is today.
- firstweekday : str
- first day of the week: “monday” or “sunday”
- showweeknumbers : bool
- whether to display week numbers (default is True).
- showothermonthdays : bool
- whether to display the last days of the previous month and the first of the next month (default is True).
- locale : str
- locale to use, e.g. ‘en_US’
- selectmode : str
- “none” or “day” (default): whether the user can change the selected day with a mouse click.
- textvariable : StringVar
- connect the currently selected date to the variable.
Style Options
- background : str
- background color of calendar border and month/year name
- foreground : str
- foreground color of month/year name
- bordercolor : str
- day border color
- headersbackground : str
- background color of day names and week numbers
- headersforeground : str
- foreground color of day names and week numbers
- selectbackground : str
- background color of selected day
- selectforeground : str
- foreground color of selected day
- disabledselectbackground : str
- background color of selected day in disabled state
- disabledselectforeground : str
- foreground color of selected day in disabled state
- normalbackground : str
- background color of normal week days
- normalforeground : str
- foreground color of normal week days
- weekendbackground : str
- background color of week-end days
- weekendforeground : str
- foreground color of week-end days
- othermonthforeground : str
- foreground color of normal week days belonging to the previous/next month
- othermonthbackground : str
- background color of normal week days belonging to the previous/next month
- othermonthweforeground : str
- foreground color of week-end days belonging to the previous/next month
- othermonthwebackground : str
- background color of week-end days belonging to the previous/next month
- disableddaybackground : str
- background color of days in disabled state
- disableddayforeground : str
- foreground color of days in disabled state
Tooltip Options (for calevents)
- tooltipforeground : str
- tooltip text color
- tooltipbackground : str
- tooltip background color
- tooltipalpha : float
- tooltip opacity between 0 and 1
- tooltipdelay : int
- delay in ms before displaying the tooltip
-
calevent_configure
(ev_id, **kw)[source]¶ Configure the event ev_id.
Keyword options: date, text, tags (see calevent_create options).
-
calevent_create
(date, text, tags=[])[source]¶ Add new event in calendar and return event id.
Options:
- date : datetime.date or datetime.datetime instance.
- event date
- text : str
- text to put in the tooltip associated to date.
- tags : list
- list of tags to apply to the event. The last tag determines the way the event is displayed. If there are several events on the same day, the lowest one (on the tooltip list) which has tags determines the colors of the day.
-
calevent_lower
(ev_id, below=None)[source]¶ Lower event ev_id in tooltip event list.
- below : str
- put ev_id below given one, if below is None, put it at the bottom of tooltip event list.
The day’s colors are determined by the last tag of the lowest event which has tags.
-
calevent_raise
(ev_id, above=None)[source]¶ Raise event ev_id in tooltip event list.
- above : str
- put ev_id above given one, if above is None, put it on top of tooltip event list.
The day’s colors are determined by the last tag of the lowest event which has tags.
-
calevent_remove
(*ev_ids, **kw)[source]¶ Remove events from calendar.
Arguments: event ids to remove or ‘all’ to remove them all.
Keyword arguments: tag, date.
They are taken into account only if no id is given. Remove all events with given tag on given date. If only date is given, remove all events on date and if only tag is given, remove all events with tag.
-
format_date
(date=None)[source]¶ Convert date (datetime.date) to a string in the locale (short format).
-
get_calevents
(date=None, tag=None)[source]¶ Return event ids of events with given tag and on given date.
If only date is given, return event ids of all events on date. If only tag is given, return event ids of all events with tag. If both options are None, return all event ids.
-
get_displayed_month
()[source]¶ Return the currently displayed month in the form of a (month, year) tuple.
-
selection_get
()[source]¶ Return currently selected date (datetime.date instance). Always return None if selectmode is “none”.
-
selection_set
(date)[source]¶ Set the selection to date.
date can be either a datetime.date instance or a string corresponding to the date format “%x” in the Calendar locale.
Do nothing if selectmode is “none”.
-
Virtual Events¶
- A
<<CalendarSelected>>
event is generated each time the user selects a day with the mouse. - A
<<CalendarMonthChanged>>
event is generated each time the user changes the displayed month.
Calendar Events¶
Special events (e.g. birthdays, ..) can be managed using the
calevent_..()
methods. The way they are displayed in the calendar is
determined with tags. An id is attributed to each event upon creation
and can be used to edit the event (ev_id argument).
DateEntry¶
Class¶
-
class
tkcalendar.
DateEntry
(master=None, **kw)[source]¶ Bases:
tkinter.ttk.Entry
Date selection entry with drop-down calendar.
-
__init__
(master=None, **kw)[source]¶ Create an entry with a drop-down calendar to select a date.
When the entry looses focus, if the user input is not a valid date, the entry content is reset to the last valid date.
Keyword Options
ttk.Entry
options:class, cursor, style, takefocus, xscrollcommand, exportselection, justify, show, state, textvariable, width.
Calendar
options: see the documentation.
-
config
(**kw)[source]¶ Configure resources of a widget.
The values for resources are specified as keyword arguments. To get an overview about the allowed keyword arguments call the method keys.
-
configure
(**kw)[source]¶ Configure resources of a widget.
The values for resources are specified as keyword arguments. To get an overview about the allowed keyword arguments call the method keys.
-
Virtual Events¶
A <<DateEntrySelected>>
event is generated each time the user selects a date.
Example¶
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
import Tkinter as tk
import ttk
from tkcalendar import Calendar, DateEntry
def example1():
def print_sel():
print(cal.selection_get())
top = tk.Toplevel(root)
cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US',
cursor="hand1", year=2018, month=2, day=5)
cal.pack(fill="both", expand=True)
ttk.Button(top, text="ok", command=print_sel).pack()
def example2():
top = tk.Toplevel(root)
cal = Calendar(top, selectmode='none')
date = cal.datetime.today() + cal.timedelta(days=2)
cal.calevent_create(date, 'Hello World', 'message')
cal.calevent_create(date, 'Reminder 2', 'reminder')
cal.calevent_create(date + cal.timedelta(days=-2), 'Reminder 1', 'reminder')
cal.calevent_create(date + cal.timedelta(days=3), 'Message', 'message')
cal.tag_config('reminder', background='red', foreground='yellow')
cal.pack(fill="both", expand=True)
ttk.Label(top, text="Hover over the events.").pack()
def example3():
top = tk.Toplevel(root)
ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
cal = DateEntry(top, width=12, background='darkblue',
foreground='white', borderwidth=2, year=2010)
cal.pack(padx=10, pady=10)
root = tk.Tk()
ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10)
ttk.Button(root, text='Calendar with events', command=example2).pack(padx=10, pady=10)
ttk.Button(root, text='DateEntry', command=example3).pack(padx=10, pady=10)
root.mainloop()
HowTos¶
Widget styling¶
Calendar
All styling is done using options, see the documentation.
DateEntry
DateEntry
inherits from ttk.Entry
therefore the styling is done using
a ttk style:
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
import Tkinter as tk
import ttk
from tkcalendar import DateEntry
style = ttk.Style()
# style.theme_use('clam') # -> uncomment this line if the styling does not work
style.configure('my.DateEntry',
fieldbackground='light green',
background='dark green',
foreground='dark blue',
arrowcolor='white')
dateentry = DateEntry(style='my.DateEntry')
dateentry.pack()
tk.mainloop()
If the style of the DateEntry
does not change, then it might be because of the
used ttk theme. Changing the theme with style.theme_use('clam')
should solve
the issue.
PyInstaller¶
When bundling an application with PyInstaller,
there seem to be an issue (#32)
with the detection of the babel dependency of tkcalendar.
This can be fixed by using the --hidden-import
option:
$ pyinstaller --hidden-import babel.numbers myscript.py
or by editing the .spec file:
hiddenimports=["babel.numbers"]
Changelog¶
tkcalendar 1.4.0¶
New features
<<CalendarMonthChanged>>
virtual event: event generated each time the user changes the displayed monthCalendar.get_displayed_month()
method: return the currently displayed month in the form of a (month, year) tuple.
New Calendar options
- showothermonthdays: show/hide the last and first days of the previous and next months
Bug fixes
- Fix handling of style option in DateEntry
- Fix display of events for January days showing on December and conversely
tkcalendar 1.3.0¶
New feature
- Add possibility to display special events (like birthdays, ..) in the calendar. The events are displayed with colors defined by tags and the event description is displayed in a tooltip (see Calendar Events)
New Calendar options
- showwekknumbers: show/hide week numbers
- firstweekday: first week day (‘monday’ or ‘sunday’)
Bug fixes
- No longer set locale globally to avoid conflicts between several instances, use babel module instead
- Make
DateEntry
compatible with more ttk themes, especially OSX default theme
tkcalendar 1.2.0¶
New Calendar options
- textvariable: connect the currently selected date to the given
StringVar
- state: ‘normal’ or ‘disabled’
- disabledselectbackground, disabledselectforeground, disableddaybackground and disableddayforeground: configure colors when Calendar is disabled
Bug fixes
- Fix
DateEntry
behavior in readonly mode - Make
Calendar.selection_get()
always return adatetime.date
tkcalendar 1.1.4¶
Bug fixes
- Fix error in january due to week 53
- Fix
DateEntry
for ttk themes other than ‘clam’
tkcalendar 1.1.3¶
Bug fixes
- Make
DateEntry
support initialisation with partial dates (e.g. just year=2010) - Improve handling of wrong year-month-day combinations
tkcalendar 1.1.0¶
Bug fixes
- Fix display of the first days of the next month
- Increment year when going from December to January
New widget
DateEntry
, date selection entry with drop-down calendar
New Calendar options
- borderwidth: width of the border around the calendar (integer)
- othermonthbackground: background color for normal week days belonging to the previous/next month
- othermonthweforeground: foreground color for week-end days belonging to the previous/next month
- othermonthwebackground: background color for week-end days belonging to the previous/next month
tkcalendar 1.0.0¶
Initial version
Index¶
License¶
tkcalendar - Calendar and DateEntry widgets for Tkinter
Copyright (C) 2017-2018 Juliette Monsel with contributions from:
tkcalendar is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
tkcalendar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.