Tkinter的日历和日期条目小部件
tkcalendar的Python项目详细描述
TKCalendar是一个Python模块,它为Tkinter提供日历和DateEntry小部件。 DateEntry小部件类似于组合框,但下拉列表不是列表,而是用于选择日期的日历。 事件可以用自定义颜色显示在日历中,工具提示显示给定日期的事件列表。 TKCalendar与Python2和Python3都兼容。 它支持许多语言环境设置(例如“fr_fr”、“en_us”…)并且颜色是可自定义的。
文档也可以在这里获得:https://tkcalendar.readthedocs.io
目录
Installation
ubuntu:使用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
使用pip:
$ pip install tkcalendar
Documentation
Calendar widget
Syntax
Calendar(master=None, **kw)
Widget keyword options
标准选项
- cursor :str
cursor to display when the pointer is in the widget
- font :str such as “Arial 20 bold” or a Tkinter Font instance
font of the calendar
- borderwidth :int
width of the border around the calendar
- state :str
“normal” or “disabled” (unresponsive widget)
小部件特定选项:
- 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 :“monday” or “sunday”
first day of the week
- weekenddays :list
days to be displayed as week-end days given as a list of integers corresponding to the number of the day in the week (e.g. [6, 7] for the last two days of the week).
- mindate :datetime.date or datetime.datetime (default is None)
minimum allowed date
- maxdate :datetime.date or datetime.datetime (default is None)
maximum allowed date
- showweeknumbers :bool (default is True)
whether to display week numbers.
- showothermonthdays :bool (default is True)
whether to display the last days of the previous month and the first of the next month.
- locale :str
locale to use, e.g. ‘en_US’
- date_pattern :str
date pattern used to format the date as a string. The default pattern used is ^{tt1}$’s short date format in the calendar’s locale.
A valid pattern is a combination of ‘d’, ‘m’ and ‘y’ separated by non letter characters to indicate how and in which order the year, month and day should be displayed.
d ‘d’ for the day of month number without padding, ‘dd’ for a two-digit day m ‘m’ for the month number without padding, ‘mm’ for a two-digit month y ‘yy’ for the two last digits of the year, any other number of ‘y’s for the full year with an extra padding of zero if it has less digits than the number of ‘y’s. Examples for ^{tt2}$
- ‘y-mm-dd’ → ‘2019-07-01’
- ‘m/d/yy’ → ‘7/1/19’
- selectmode :“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.
样式选项:
- background :
background color of calendar border and month/year name
- foreground :
foreground color of month/year name
- disabledbackground :str
background color of calendar border and month/year name in disabled state
- disabledforeground :str
foreground color of month/year name in disabled state
- bordercolor :
day border color
- headersbackground :
background color of day names and week numbers
- headersforeground :
foreground color of day names and week numbers
- selectbackground :
background color of selected day
- selectforeground :
foreground color of selected day
- disabledselectbackground :
background color of selected day in disabled state
- disabledselectforeground :
foreground color of selected day in disabled state
- normalbackground :
background color of normal week days
- normalforeground :
foreground color of normal week days
- weekendbackground :
background color of week-end days
- weekendforeground :
foreground color of week-end days
- othermonthforeground :
foreground color of normal week days belonging to the previous/next month
- othermonthbackground :
background color of normal week days belonging to the previous/next month
- othermonthweforeground :
foreground color of week-end days belonging to the previous/next month
- othermonthwebackground :
background color of week-end days belonging to the previous/next month
- disableddaybackground :
background color of days in disabled state
- disableddayforeground :
foreground color of days in disabled state
工具提示选项(用于日历事件):
- tooltipforeground :
tooltip text color
- tooltipbackground :
tooltip background color
- tooltipalpha :float
tooltip opacity between 0 and 1
- tooltipdelay :int
delay in ms before displaying the tooltip
Virtual Events
- A ^{tt3}$ event is generated each time the user selects a day with the mouse.
- A ^{tt4}$ event is generated each time the user changes the displayed month.
Widget methods
Standard methods:
特定于小部件的方法:
- calevent_cget(ev_id, option) :
Return value of given option for the event ev_id.
- calevent_configure(ev_id, **kw) :
Return value of given option for the event ev_id.
- calevent_create(date, text, tags=[]) :
Add new event in calendar and return event id.
Options:
date: ^{tt5}$ or ^{tt6}$ instance.
text: text to put in the tooltip associated to date.
tags: 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=无):
工具提示事件列表中的下事件{em1}$ev_id。
below: put event below given one, if below is None, put it at the bottom of tooltip event list.
一天的颜色由具有标记的最低事件的最后一个标记确定。
- Calevent_raise(ev_id,above=无):
工具提示事件列表中的引发事件{em1}$ev_id。above: put ev_id above given one, if above is None, put it on top of tooltip event list.
一天的颜色由具有标记的最低事件的最后一个标记确定。
- calevent_remove(*ev_ids,**kw):
从日历中删除事件。
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.
- 获取日期():
如果selectMode为“day”,则返回与 Calendarlocale,否则返回""。
- 获取日历事件(日期=无,标记=无):
返回具有给定标记和给定日期的事件的事件ID。
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.
- 显示月份:
以(月,年)元组的形式返回当前显示的月份。
- 见(日期):
显示date所在的月份。
date: ^{tt5}$ or ^{tt6}$ instance.
- 选择清除():
清除选择。
- 选择获取():
如果selectMode是“day”,则将所选日期返回为datetime.date 实例,否则返回None。
- 选择集(self,date):
如果selectMode是“day”,则将选择设置为date,其中date可以是datetime.date 实例或与^{tt7}中的日期格式"%x"对应的字符串$ 地点。如果selectMode是"none",则不执行任何操作。
- 标记cget(标记,选项):
返回标记选项的值。
- 标签配置(自,标签,**kw):
配置{em1}$tag。
Keyword options: foreground, background (of the day in the calendar)
- 删除标记(tag):
删除给定标记并将其从所有事件中移除。
- 标记名():
> p>返回现有标记的元组。
DateEntry widget
带有下拉日历的日期选择项。
Syntax
DateEntry(master=None, **kw)
Widget keyword options
Keyword options of ^{tt7}$ to configure the drop-down calendar
Keyword options of ^{tt18}$
By default, ‘validate’ is set to ‘focusout’ and ‘validatecommand’ is configured so that each time the widget looses focus, if the content is not a valid date (in locale format ‘%x’), it is reset to the previous valid date.
The widget style is set to ‘DateEntry’. A custom style inheritting from ‘DateEntry’ can be created by naming it ‘<style name>.DateEntry’
虚拟事件
A ^{tt19}$ event is generated each time the user selects a date.
Widget methods
Standard methods:
特定于小部件的方法:
- drop_down() :
Display or withdraw the drop-down calendar depending on its current state.
- get_date() :
Return the selected date as a ^{tt5}$ instance.
- set_date(self, date) :
Set the value of the DateEntry to date where date can be either a ^{tt5}$ instance or a string corresponding to the date format “%x” in the Calendar locale.
Changelog
TKCalendar 1.5.1
- Fix calendar drop-down not in front issue if window has the ^{tt23}$ attribute in Windows (#49)
- Make ^{tt24}$ and ^{tt25}$ accept a dictionary like standard tkinter widgets
- Fix calendar not hiding when clicking again on ^{tt26}$ drop-down button in Windows (#51)
- Fix maxdate disabled while it sould be the latest allowed date (#50)
TKCalendar 1.5.0
- Add disabledforeground and disabledbackground options to further customize the disabled state appearance of the ^{tt7}$
- Add maxdate and mindate options to set an allowed date range for date selection
- Add weekenddays option to choose the days colored as week-end days (#37)
- Add date_pattern option to customize the date format
- Add ^{tt28}$ method to make sure a date is visible
- Make ^{tt29}$ actually clear the selection
- Make locale option editable after the creation of the Calendar
- Fix ^{tt30}$ when retrieving default locale
- Fix date parsing error in Swedish locale and some others (#44)
- Improve compliance with ttk themes by making the ^{tt26}$ look like a ^{tt32}$ (#42)
- Fix high CPU issues in Windows (#36)
TKCalendar 1.4.0
- Add ^{tt4}$ virtual event to the ^{tt7}$ widget
- Add ^{tt35}$ method to the ^{tt7}$ widget
- Add showothermonthdays option to show/hide the last and first days of the previous and next months
- Fix display of events for January days showing on December page and conversely
TKCalendar 1.3.1
- Fix bug in day selection when firstweekday is ‘sunday’ (#28)
TKCalendar 1.3.0
- No longer set locale globally to avoid conflicts between several instances, use ^{tt1}$ module instead (#15)
- Add showwekknumbers option to show/hide week numbers (#18)
- Add firstweekday option to choose first week day between ‘monday’ and ‘sunday’ (#25)
- Make ^{tt26}$ compatible with more ttk themes, especially OSX default theme (#16)
- 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 documentation) (#19)
TKCalendar 1.2.1
- Fix ^{tt30}$ in ^{tt26}$ with Python 3.6.5 (#13)
TKCalendar 1.2.0
- Add textvariable option to ^{tt7}$ (#6)
- Add state (‘normal’ or ‘disabled’) option to Calendar
- Add disabledselectbackground, disabledselectforeground, disableddaybackground and disableddayforeground options to configure colors when ^{tt7}$ is disabled
- Fix ^{tt26}$ behavior in readonly mode
- Make ^{tt44}$ always return a ^{tt5}$
TKCalendar 1.1.5
- Fix endless triggering of ^{tt46}$ event in ^{tt26}$ (#9)
TKCalendar 1.1.4
- Fix error in January due to week 53
- Fix ^{tt26}$ for ttk themes other than ‘clam’ (#3)
TKCalendar 1.1.3
- Make ^{tt26}$ support initialisation with partial dates (e.g. just year=2010)
- Improve handling of wrong year-month-day combinations
TKCalendar 1.1.2
- Fix bug after destroying a ^{tt26}$
- Fix bug in style and font
TKCalendar 1.1.1
- Fix bug when content of ^{tt26}$ is not a valid date
TKCalendar 1.1.0
- Fix display of the first days of the next month
- Increment year when going from December to January
- Add widget ^{tt26}$: date selection entry with drop-down calendar
- Add borderwidth, othermonthbackground, othermonthweforeground, othermonthwebackground options to further customize the appearance of the calendar
TKCalendar 1.0.0
- Initial version
Example
fromtkcalendarimportCalendar,DateEntrytry:importtkinterastkfromtkinterimportttkexceptImportError:importTkinterastkimportttkdefexample1():defprint_sel():print(cal.selection_get())cal.see(datetime.date(year=2016,month=2,day=5))top=tk.Toplevel(root)importdatetimetoday=datetime.date.today()mindate=datetime.date(year=2018,month=1,day=21)maxdate=today+datetime.timedelta(days=5)print(mindate,maxdate)cal=Calendar(top,font="Arial 14",selectmode='day',locale='en_US',mindate=mindate,maxdate=maxdate,disabledforeground='red',cursor="hand1",year=2018,month=2,day=5)cal.pack(fill="both",expand=True)ttk.Button(top,text="ok",command=print_sel).pack()defexample2():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()defexample3():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()