-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.py
309 lines (288 loc) · 13.6 KB
/
date.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import calendar
import tkinter as tk
import tkinter.font as tkFont
from tkinter import ttk
datetime = calendar.datetime.datetime
timedelta = calendar.datetime.timedelta
class Date:
def __init__(self, point=None):
self.master = tk.Toplevel()
self.master.withdraw()
self.master.attributes('-topmost', True)
fwday = calendar.SUNDAY
year = datetime.now().year
month = datetime.now().month
locale = None
sel_bg = '#ecffc4'
sel_fg = '#05640e'
self._date = datetime(year, month, 1) #每月第一日
self._selection = None #设置为未选中日期
self.G_Frame = ttk.Frame(self.master)
self._cal = self.__get_calendar(locale, fwday)
self.__setup_styles() # 创建自定义样式
self.__place_widgets() # pack/grid 小部件
self.__config_calendar() # 调整日历列和安装标记
# 配置画布和正确的绑定,以选择日期。
self.__setup_selection(sel_bg, sel_fg)
# 存储项ID,用于稍后插入。
self._items = [self._calendar.insert('', 'end', values='') for _ in range(6)]
# 在当前空日历中插入日期
self._update()
self.G_Frame.pack(expand=1, fill='both')
self.master.overrideredirect(1)
self.master.update_idletasks()
width, height = self.master.winfo_reqwidth(), self.master.winfo_reqheight()
self.height = height
if point:
x, y = point[0], point[1]
else:
x, y = (self.master.winfo_screenwidth() -
width) / 2, (self.master.winfo_screenheight() - height) / 2
self.master.geometry('%dx%d+%d+%d' % (width, height, x, y)) #窗口位置居中
self.master.after(300, self._main_judge)
self.master.deiconify()
self.master.focus_set()
self.master.wait_window(
) #这里应该使用wait_window挂起窗口,如果使用mainloop,可能会导致主程序很多错误
def __get_calendar(self, locale, fwday):
if locale is None:
return calendar.TextCalendar(fwday)
else:
return calendar.LocaleTextCalendar(fwday, locale)
def __setitem__(self, item, value):
if item in ('year', 'month'):
raise AttributeError("attribute '%s' is not writeable" % item)
elif item == 'selectbackground':
self._canvas['background'] = value
elif item == 'selectforeground':
self._canvas.itemconfigure(self._canvas.text, item=value)
else:
self.G_Frame.__setitem__(self, item, value)
def __getitem__(self, item):
if item in ('year', 'month'):
return getattr(self._date, item)
elif item == 'selectbackground':
return self._canvas['background']
elif item == 'selectforeground':
return self._canvas.itemcget(self._canvas.text, 'fill')
else:
r = ttk.tclobjs_to_py({item: ttk.Frame.__getitem__(self, item)})
return r[item]
def __setup_styles(self):
# 自定义TTK风格
style = ttk.Style(self.master)
arrow_layout = lambda dir: ([('Button.focus', {
'children': [('Button.%sarrow' % dir, None)]
})])
style.layout('L.TButton', arrow_layout('left'))
style.layout('R.TButton', arrow_layout('right'))
def __place_widgets(self):
# 标头框架及其小部件
Input_judgment_num = self.master.register(
self.Input_judgment) # 需要将函数包装一下,必要的
hframe = ttk.Frame(self.G_Frame)
gframe = ttk.Frame(self.G_Frame)
bframe = ttk.Frame(self.G_Frame)
hframe.pack(in_=self.G_Frame, side='top', pady=5, anchor='center')
gframe.pack(in_=self.G_Frame, fill=tk.X, pady=5)
bframe.pack(in_=self.G_Frame, side='bottom', pady=5)
lbtn = ttk.Button(hframe, style='L.TButton', command=self._prev_month)
lbtn.grid(in_=hframe, column=0, row=0, padx=12)
rbtn = ttk.Button(hframe, style='R.TButton', command=self._next_month)
rbtn.grid(in_=hframe, column=5, row=0, padx=12)
self.CB_year = ttk.Combobox(
hframe,
width=5,
values=[
str(year) for year in range(datetime.now().year,
datetime.now().year - 11, -1)
],
validate='key',
validatecommand=(Input_judgment_num, '%P'))
self.CB_year.current(0)
self.CB_year.grid(in_=hframe, column=1, row=0)
self.CB_year.bind('<KeyPress>', lambda event: self._update(event, True))
self.CB_year.bind("<<ComboboxSelected>>", self._update)
tk.Label(hframe, text='年', justify='left').grid(in_=hframe,
column=2,
row=0,
padx=(0, 5))
self.CB_month = ttk.Combobox(
hframe,
width=3,
values=['%02d' % month for month in range(1, 13)],
state='readonly')
self.CB_month.current(datetime.now().month - 1)
self.CB_month.grid(in_=hframe, column=3, row=0)
self.CB_month.bind("<<ComboboxSelected>>", self._update)
tk.Label(hframe, text='月', justify='left').grid(in_=hframe,
column=4,
row=0)
# 日历部件
self._calendar = ttk.Treeview(gframe,
show='',
selectmode='none',
height=7)
self._calendar.pack(expand=1, fill='both', side='bottom', padx=5)
ttk.Button(bframe, text="确 定", width=6,
command=lambda: self._exit(True)).grid(row=0,
column=0,
sticky='ns',
padx=20)
ttk.Button(bframe, text="取 消", width=6,
command=self._exit).grid(row=0, column=1, sticky='ne', padx=20)
tk.Frame(self.G_Frame, bg='#565656').place(x=0,
y=0,
relx=0,
rely=0,
relwidth=1,
relheigh=2 / 200)
tk.Frame(self.G_Frame, bg='#565656').place(x=0,
y=0,
relx=0,
rely=198 / 200,
relwidth=1,
relheigh=2 / 200)
tk.Frame(self.G_Frame, bg='#565656').place(x=0,
y=0,
relx=0,
rely=0,
relwidth=2 / 200,
relheigh=1)
tk.Frame(self.G_Frame, bg='#565656').place(x=0,
y=0,
relx=198 / 200,
rely=0,
relwidth=2 / 200,
relheigh=1)
def __config_calendar(self):
# cols = self._cal.formatweekheader(3).split()
cols = ['日', '一', '二', '三', '四', '五', '六']
self._calendar['columns'] = cols
self._calendar.tag_configure('header', background='grey90')
self._calendar.insert('', 'end', values=cols, tag='header')
# 调整其列宽
font = tkFont.Font()
maxwidth = max(font.measure(col) for col in cols)
for col in cols:
self._calendar.column(col,
width=maxwidth,
minwidth=maxwidth,
anchor='center')
def __setup_selection(self, sel_bg, sel_fg):
def __canvas_forget(evt):
canvas.place_forget()
self._selection = None
self._font = tkFont.Font()
self._canvas = canvas = tk.Canvas(self._calendar,
background=sel_bg,
borderwidth=0,
highlightthickness=0)
canvas.text = canvas.create_text(0, 0, fill=sel_fg, anchor='w')
canvas.bind('<Button-1>', __canvas_forget)
self._calendar.bind('<Configure>', __canvas_forget)
self._calendar.bind('<Button-1>', self._pressed)
def _build_calendar(self):
year, month = self._date.year, self._date.month
header = self._cal.formatmonthname(year, month, 0)
# 更新日历显示的日期
cal = self._cal.monthdayscalendar(year, month)
for indx, item in enumerate(self._items):
week = cal[indx] if indx < len(cal) else []
fmt_week = [('%02d' % day) if day else '' for day in week]
self._calendar.item(item, values=fmt_week)
def _show_select(self, text, bbox):
x, y, width, height = bbox
textw = self._font.measure(text)
canvas = self._canvas
canvas.configure(width=width, height=height)
canvas.coords(canvas.text, (width - textw) / 2, height / 2 - 1)
canvas.itemconfigure(canvas.text, text=text)
canvas.place(in_=self._calendar, x=x, y=y)
def _pressed(self, evt=None, item=None, column=None, widget=None):
"""在日历的某个地方点击。"""
if not item:
x, y, widget = evt.x, evt.y, evt.widget
item = widget.identify_row(y)
column = widget.identify_column(x)
if not column or not item in self._items:
# 在工作日行中单击或仅在列外单击。
return
item_values = widget.item(item)['values']
if not len(item_values): # 这个月的行是空的。
return
text = item_values[int(column[1]) - 1]
if not text:
return
bbox = widget.bbox(item, column)
if not bbox: # 日历尚不可见
self.master.after(
20,
lambda: self._pressed(item=item, column=column, widget=widget))
return
text = '%02d' % text
self._selection = (text, item, column)
self._show_select(text, bbox)
def _prev_month(self):
"""更新日历以显示前一个月。"""
self._canvas.place_forget()
self._selection = None
self._date = self._date - timedelta(days=1)
self._date = datetime(self._date.year, self._date.month, 1)
self.CB_year.set(self._date.year)
self.CB_month.set(self._date.month)
self._update()
def _next_month(self):
"""更新日历以显示下一个月。"""
self._canvas.place_forget()
self._selection = None
year, month = self._date.year, self._date.month
self._date = self._date + timedelta(
days=calendar.monthrange(year, month)[1] + 1)
self._date = datetime(self._date.year, self._date.month, 1)
self.CB_year.set(self._date.year)
self.CB_month.set(self._date.month)
self._update()
def _update(self, event=None, key=None):
"""刷新界面"""
if key and event.keysym != 'Return': return
year = int(self.CB_year.get())
month = int(self.CB_month.get())
if year == 0 or year > 9999: return
self._canvas.place_forget()
self._date = datetime(year, month, 1)
self._build_calendar() # 重建日历
if year == datetime.now().year and month == datetime.now().month:
day = datetime.now().day
for _item, day_list in enumerate(
self._cal.monthdayscalendar(year, month)):
if day in day_list:
item = 'I00' + str(_item + 2)
column = '#' + str(day_list.index(day) + 1)
self.master.after(
100, lambda: self._pressed(
item=item, column=column, widget=self._calendar))
def _exit(self, confirm=False):
if not confirm: self._selection = None
self.master.destroy()
def _main_judge(self):
"""判断窗口是否在最顶层"""
try:
if self.master.focus_displayof() == None or 'toplevel' not in str(
self.master.focus_displayof()):
self._exit()
else:
self.master.after(10, self._main_judge)
except:
self.master.after(10, self._main_judge)
def selection(self):
"""返回表示当前选定日期的日期时间。"""
if not self._selection: return None
year, month = self._date.year, self._date.month
return str(datetime(year, month, int(self._selection[0])))[:10]
def Input_judgment(self, content):
"""输入判断"""
if content.isdigit() or content == "":
return True
else:
return False