-
Notifications
You must be signed in to change notification settings - Fork 8
/
calendarium.py
154 lines (114 loc) · 4.23 KB
/
calendarium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
"""Provides a primitive light widget to manage calendar date in tkinter projects.
How import;
from calendarium import Calendarium
How instantiate in your frame:
self.start_date = Calendarium(self,"Start Date")
How pack:
#f is a tkinter widget such as Frame,LabelFrame
if use grid method
self.start_date.get_calendarium(f, row, col)
If use pack method
self.start_date.get_calendarium(f,)
Set today date:
self.start_date.set_today()
Check if a date is right formated:
if self.start_date.get_date(self)==False:return
Notice that in the spinbox widget we allowed only integers.
Calendarium use datetime.date to set/get date.
"""
import sys
import datetime
from datetime import date
import tkinter as tk
from tkinter import messagebox
__author__ = "1966bc aka giuseppe costanzi"
__copyright__ = "Copyleft"
__credits__ = ["hal9000",]
__license__ = "GNU GPL Version 3, 29 June 2007"
__version__ = "1.0"
__maintainer__ = "1966bc"
__email__ = "giuseppecostanzi@gmail.com"
__date__ = "2019-08-26"
__status__ = "Beta"
class Calendarium(tk.Frame):
def __init__(self, caller, name):
super().__init__()
self.vcmd = (self.register(self.validate), '%d', '%P', '%S')
self.caller = caller
self.name = name
self.day = tk.IntVar()
self.month = tk.IntVar()
self.year = tk.IntVar()
def __str__(self):
return "class: %s" % (self.__class__.__name__, )
def get_calendarium(self, container, row=None, col=None):
w = tk.LabelFrame(container,
text=self.name,
borderwidth=1,
padx=2, pady=2,
relief=tk.GROOVE,)
day_label = tk.LabelFrame(w, text="Day")
d = tk.Spinbox(day_label, bg='white', fg='blue', width=2,
from_=1, to=31,
validate='key',
validatecommand=self.vcmd,
textvariable=self.day,
relief=tk.GROOVE,)
month_label = tk.LabelFrame(w, text="Month")
m = tk.Spinbox(month_label, bg='white', fg='blue', width=2,
from_=1, to=12,
validate='key',
validatecommand=self.vcmd,
textvariable=self.month,
relief=tk.GROOVE,)
year_label = tk.LabelFrame(w, text="Year")
y = tk.Spinbox(year_label, bg='white', fg='blue', width=4,
validate='key',
validatecommand=self.vcmd,
from_=1900, to=3000,
textvariable=self.year,
relief=tk.GROOVE,)
for p, i in enumerate((day_label , d, month_label, m, year_label, y)):
if row is not None:
i.grid(row=0, column=p, padx=5, pady=5, sticky=tk.W)
else:
i.pack(side=tk.LEFT, fill=tk.X, padx=2)
if row is not None:
w.grid(row=row, column=col, sticky=tk.W)
else:
w.pack()
return w
def set_today(self,):
today = date.today()
self.day.set(today.day)
self.month.set(today.month)
self.year.set(today.year)
def get_date(self, caller):
try:
return datetime.date(self.year.get(), self.month.get(), self.day.get())
except ValueError:
msg = "Date format error:\n%s"%str(sys.exc_info()[1])
messagebox.showerror(caller.title(), msg, parent=caller)
return False
def get_timestamp(self,):
t = datetime.datetime.now()
return datetime.datetime(self.year.get(),
self.month.get(),
self.day.get(),
t.hour,
t.minute,
t.second)
def validate(self, action, value, text,):
# action=1 -> insert
if action == '1':
if text in '0123456789':
try:
int(value)
return True
except ValueError:
return False
else:
return False
else:
return True