pygui_simple 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pygui_simple/__init__.py +0 -0
- pygui_simple/cv2_utilities.py +162 -0
- pygui_simple/py.typed +0 -0
- pygui_simple/tkcalendar.py +616 -0
- pygui_simple/tkcontrol.py +112 -0
- pygui_simple/tkmatplot.py +294 -0
- pygui_simple/tkscrollpicker.py +617 -0
- pygui_simple/tkslideswitch.py +448 -0
- pygui_simple/tkwin.py +1344 -0
- pygui_simple/winbasic.py +327 -0
- pygui_simple-0.1.0.dist-info/METADATA +17 -0
- pygui_simple-0.1.0.dist-info/RECORD +14 -0
- pygui_simple-0.1.0.dist-info/WHEEL +4 -0
- pygui_simple-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
#!/usr/bin/python3
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
import datetime
|
|
5
|
+
import calendar
|
|
6
|
+
import tkinter as tk
|
|
7
|
+
from tkinter import ttk
|
|
8
|
+
from typing import override, cast
|
|
9
|
+
from typing import TypeAlias, Callable, Any
|
|
10
|
+
from typing import Generic, TypeVar
|
|
11
|
+
|
|
12
|
+
from pygui.winbasic import Dialog
|
|
13
|
+
from pygui.tkcontrol import tkControl
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
T = TypeVar("T", bound=int | str)
|
|
17
|
+
|
|
18
|
+
OnSelect: TypeAlias = Callable[[T], None]
|
|
19
|
+
|
|
20
|
+
class ScrollPicker(tk.Frame, Generic[T]):
|
|
21
|
+
def __init__(self, parent: tk.Tk | tk.Frame,
|
|
22
|
+
val_list: list[T],
|
|
23
|
+
initial: T | None = None,
|
|
24
|
+
on_select: OnSelect[T] | None = None,
|
|
25
|
+
**kwargs: Any) -> None:
|
|
26
|
+
super().__init__(parent, **kwargs)
|
|
27
|
+
self._parent: tk.Tk | tk.Frame = parent
|
|
28
|
+
|
|
29
|
+
self._val_list: list[T] = val_list
|
|
30
|
+
self._on_select: OnSelect[T] | None = on_select
|
|
31
|
+
if initial:
|
|
32
|
+
self._selected_data: T = initial
|
|
33
|
+
else:
|
|
34
|
+
self._selected_data = val_list[0]
|
|
35
|
+
|
|
36
|
+
self._background: str = cast(str, kwargs.get('background', 'white'))
|
|
37
|
+
|
|
38
|
+
self._index: int = 0
|
|
39
|
+
|
|
40
|
+
# 创建UI组件
|
|
41
|
+
self._create_widgets()
|
|
42
|
+
# 绑定鼠标事件
|
|
43
|
+
self._bind_mouse_scroll()
|
|
44
|
+
# 初始化预览内容
|
|
45
|
+
self._update_previews()
|
|
46
|
+
|
|
47
|
+
self._data_up2: tk.Label
|
|
48
|
+
self._data_up1: tk.Label
|
|
49
|
+
self._data_label: tk.Label
|
|
50
|
+
self._data_down1: tk.Label
|
|
51
|
+
self._data_down2: tk.Label
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def data(self):
|
|
55
|
+
return self._selected_data
|
|
56
|
+
|
|
57
|
+
def _create_widgets(self) -> None:
|
|
58
|
+
# 配置网格权重,使布局更美观
|
|
59
|
+
for i in range(5): # 行
|
|
60
|
+
_ = self.grid_rowconfigure(i, weight=1)
|
|
61
|
+
for i in range(1): # 列
|
|
62
|
+
_ = self.grid_columnconfigure(i, weight=1)
|
|
63
|
+
|
|
64
|
+
# 上方预览(+2)
|
|
65
|
+
self._data_up2 = tk.Label(self, font=("SimHei", 10), fg="#888888", width=6,
|
|
66
|
+
bg=self._background,
|
|
67
|
+
anchor="center")
|
|
68
|
+
self._data_up2.grid(row=0, column=0)
|
|
69
|
+
|
|
70
|
+
# 上方预览(+1)
|
|
71
|
+
self._data_up1 = tk.Label(self, font=("SimHei", 12), fg="#555555", width=6,
|
|
72
|
+
bg=self._background,
|
|
73
|
+
anchor="center")
|
|
74
|
+
self._data_up1.grid(row=1, column=0)
|
|
75
|
+
|
|
76
|
+
# 当前选中值
|
|
77
|
+
self._data_label = tk.Label(
|
|
78
|
+
self,
|
|
79
|
+
text=str(self._selected_data),
|
|
80
|
+
font=("SimHei", 16, "bold"),
|
|
81
|
+
relief="solid",
|
|
82
|
+
width=8,
|
|
83
|
+
height=2,
|
|
84
|
+
anchor="center",
|
|
85
|
+
bg=self._background
|
|
86
|
+
)
|
|
87
|
+
self._data_label.grid(row=2, column=0, padx=10)
|
|
88
|
+
|
|
89
|
+
# 下方预览(-1)
|
|
90
|
+
self._data_down1 = tk.Label(self, font=("SimHei", 12), fg="#555555", width=6,
|
|
91
|
+
bg=self._background,
|
|
92
|
+
anchor="center")
|
|
93
|
+
self._data_down1.grid(row=3, column=0)
|
|
94
|
+
|
|
95
|
+
# 下方预览(-2)
|
|
96
|
+
self._data_down2 = tk.Label(self, font=("SimHei", 10), fg="#888888", width=6,
|
|
97
|
+
bg=self._background,
|
|
98
|
+
anchor="center")
|
|
99
|
+
self._data_down2.grid(row=4, column=0)
|
|
100
|
+
|
|
101
|
+
def _bind_mouse_scroll(self) -> None: # 保护方法
|
|
102
|
+
""" 绑定鼠标滚轮事件"""
|
|
103
|
+
|
|
104
|
+
scrollable_data_widgets = [self._data_label, self._data_up1, self._data_up2,
|
|
105
|
+
self._data_down1, self._data_down2]
|
|
106
|
+
for widget in scrollable_data_widgets:
|
|
107
|
+
_ = widget.bind("<MouseWheel>", self._on_data_scroll)
|
|
108
|
+
_ = widget.bind("<Button-4>", self._on_data_scroll)
|
|
109
|
+
_ = widget.bind("<Button-5>", self._on_data_scroll)
|
|
110
|
+
|
|
111
|
+
# 鼠标悬停效果
|
|
112
|
+
_ = self._data_label.bind("<Enter>", self._on_enter)
|
|
113
|
+
_ = self._data_label.bind("<Leave>", self._on_leave)
|
|
114
|
+
|
|
115
|
+
def _on_enter(self, event: tk.Event[tk.Label]) -> None: # 保护方法
|
|
116
|
+
""" 鼠标进入时的样式变化"""
|
|
117
|
+
if hasattr(event.widget, 'config'):
|
|
118
|
+
_ = event.widget.config(bg="#e0f0ff")
|
|
119
|
+
|
|
120
|
+
def _on_leave(self, event: tk.Event[tk.Label]) -> None: # 保护方法
|
|
121
|
+
""" 鼠标离开时的样式恢复"""
|
|
122
|
+
if hasattr(event.widget, 'config'):
|
|
123
|
+
_ = event.widget.config(bg="white")
|
|
124
|
+
|
|
125
|
+
def _update_previews(self) -> None: # 保护方法
|
|
126
|
+
""" 更新上下预览内容"""
|
|
127
|
+
# 上方预览(+1和+2,受限于上限)
|
|
128
|
+
up1 = self._get_data(self._index + 1)
|
|
129
|
+
up2 = self._get_data(self._index + 2)
|
|
130
|
+
|
|
131
|
+
# 下方预览(-1和-2,受限于下限)
|
|
132
|
+
down1 = self._get_data(self._index - 1)
|
|
133
|
+
down2 = self._get_data(self._index - 2)
|
|
134
|
+
|
|
135
|
+
_ = self._data_up2.config(text=str(up2))
|
|
136
|
+
_ = self._data_up1.config(text=str(up1))
|
|
137
|
+
_ = self._data_down1.config(text=str(down1))
|
|
138
|
+
_ = self._data_down2.config(text=str(down2))
|
|
139
|
+
|
|
140
|
+
def _on_data_scroll(self, event: tk.Event) -> None: # 保护方法
|
|
141
|
+
""" 处理滚动"""
|
|
142
|
+
if event.delta > 0 or getattr(event, 'num', 0) == 4: # 上滚增加
|
|
143
|
+
self._selected_data = self._get_data(self._index + 1, True)
|
|
144
|
+
else: # 下滚减少
|
|
145
|
+
self._selected_data = self._get_data(self._index - 1, True)
|
|
146
|
+
|
|
147
|
+
_ = self._data_label.config(text=str(self._selected_data))
|
|
148
|
+
|
|
149
|
+
self._update_previews()
|
|
150
|
+
|
|
151
|
+
if self._on_select is not None:
|
|
152
|
+
self._on_select(self._selected_data)
|
|
153
|
+
|
|
154
|
+
def _get_data(self, index: int, update_index: int = False):
|
|
155
|
+
val_len = len(self._val_list)
|
|
156
|
+
if index >= val_len:
|
|
157
|
+
index -= val_len
|
|
158
|
+
elif index < 0:
|
|
159
|
+
index += val_len
|
|
160
|
+
if update_index:
|
|
161
|
+
self._index = index
|
|
162
|
+
return self._val_list[index]
|
|
163
|
+
|
|
164
|
+
def update_data(self, data: T):
|
|
165
|
+
self._selected_data = data
|
|
166
|
+
self._update_previews()
|
|
167
|
+
|
|
168
|
+
def update_val_list(self, val_list: list[T]):
|
|
169
|
+
self._val_list = val_list
|
|
170
|
+
self._update_previews()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class ScrollPickerCtrl(tkControl, Generic[T]):
|
|
174
|
+
def __init__(self, parent: tk.Misc, owner: Dialog, idself: str,
|
|
175
|
+
width: int, height: int,
|
|
176
|
+
val_list: list[T],
|
|
177
|
+
initial: T | None = None,
|
|
178
|
+
background: str = "white"):
|
|
179
|
+
self._master: tk.Misc = parent
|
|
180
|
+
self._owner: Dialog = owner
|
|
181
|
+
|
|
182
|
+
self._frame: tk.Frame = tk.Frame(self._master,
|
|
183
|
+
background=background,
|
|
184
|
+
width=width, height=height)
|
|
185
|
+
super().__init__(parent, "", idself, self._frame)
|
|
186
|
+
|
|
187
|
+
self._scrollpicker: ScrollPicker[T] = ScrollPicker[T](self._frame, val_list,
|
|
188
|
+
initial, self._on_val_change,
|
|
189
|
+
background=background
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
self._selected_val: T = initial if initial else val_list[0]
|
|
193
|
+
|
|
194
|
+
# 创建UI组件
|
|
195
|
+
self._create_widgets()
|
|
196
|
+
|
|
197
|
+
def _create_widgets(self) -> None:
|
|
198
|
+
_ = self._frame.grid_rowconfigure(0, weight=1)
|
|
199
|
+
_ = self._frame.grid_columnconfigure(0, weight=1)
|
|
200
|
+
|
|
201
|
+
self._scrollpicker.grid(row=0, column=1)
|
|
202
|
+
|
|
203
|
+
def _on_val_change(self, val: T):
|
|
204
|
+
self._selected_val = val
|
|
205
|
+
_ = self._owner.process_message(self._idself, event="select_val", val=val)
|
|
206
|
+
|
|
207
|
+
def get_val(self):
|
|
208
|
+
""" 获取当前选择的时间"""
|
|
209
|
+
return self._selected_val
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class DateScrollPickerDialog:
|
|
213
|
+
def __init__(self, point: tuple[int, int] | None = None,
|
|
214
|
+
title: str = "", initial: str = ""):
|
|
215
|
+
self._master: tk.Toplevel = tk.Toplevel()
|
|
216
|
+
self._master.withdraw()
|
|
217
|
+
|
|
218
|
+
self._frame: tk.Frame = tk.Frame(self._master)
|
|
219
|
+
|
|
220
|
+
self._title: str = title if title else "日期选择器"
|
|
221
|
+
|
|
222
|
+
# 初始日期设置
|
|
223
|
+
if not initial:
|
|
224
|
+
today: datetime.datetime = datetime.datetime.today()
|
|
225
|
+
self._selected_year: int = today.year
|
|
226
|
+
self._selected_month: int = today.month
|
|
227
|
+
self._selected_day: int = today.day
|
|
228
|
+
else:
|
|
229
|
+
today_list = initial.split("-")
|
|
230
|
+
self._selected_year = int(today_list[0])
|
|
231
|
+
self._selected_month = int(today_list[1])
|
|
232
|
+
self._selected_day = int(today_list[2])
|
|
233
|
+
|
|
234
|
+
year_list = list(range(1970, 2101))
|
|
235
|
+
self._year_scrollpicker: ScrollPicker[int] = ScrollPicker[int](self._frame,
|
|
236
|
+
year_list, self._selected_year,
|
|
237
|
+
self._on_year_change)
|
|
238
|
+
|
|
239
|
+
month_list = list(range(1, 13))
|
|
240
|
+
self._month_scrollpicker: ScrollPicker[int] = ScrollPicker[int](self._frame,
|
|
241
|
+
month_list, self._selected_month,
|
|
242
|
+
self._on_month_change)
|
|
243
|
+
|
|
244
|
+
day_list = list(range(1, 32))
|
|
245
|
+
self._day_scrollpicker: ScrollPicker[int] = ScrollPicker[int](self._frame,
|
|
246
|
+
day_list, self._selected_day,
|
|
247
|
+
self._on_day_change)
|
|
248
|
+
|
|
249
|
+
# 创建UI组件
|
|
250
|
+
self._create_widgets()
|
|
251
|
+
|
|
252
|
+
self._confirm_btn: ttk.Button
|
|
253
|
+
self._result_var: tk.StringVar
|
|
254
|
+
self._result_label: ttk.Label
|
|
255
|
+
|
|
256
|
+
self._frame.pack(expand = 1, fill = 'both')
|
|
257
|
+
|
|
258
|
+
self._master.overrideredirect(True)
|
|
259
|
+
self._master.update_idletasks()
|
|
260
|
+
width, height = self._master.winfo_reqwidth(), self._master.winfo_reqheight()
|
|
261
|
+
self.height: int = height
|
|
262
|
+
if point:
|
|
263
|
+
x, y = point[0], point[1]
|
|
264
|
+
else:
|
|
265
|
+
x, y = (self._master.winfo_screenwidth() - width)/2, \
|
|
266
|
+
(self._master.winfo_screenheight() - height)/2
|
|
267
|
+
self._master.geometry(f"{width}x{height}+{x}+{y}") # 窗口位置居中
|
|
268
|
+
_ = self._master.after(300, self._main_judge)
|
|
269
|
+
self._master.attributes('-topmost', True)
|
|
270
|
+
self._master.grab_set() # ensure all input goes to our window
|
|
271
|
+
self._master.deiconify()
|
|
272
|
+
self._master.focus_set()
|
|
273
|
+
self._master.wait_window()
|
|
274
|
+
|
|
275
|
+
def _create_widgets(self) -> None:
|
|
276
|
+
# 配置网格权重,使布局更美观
|
|
277
|
+
for i in range(3): # 行
|
|
278
|
+
_ = self._frame.grid_rowconfigure(i, weight=1)
|
|
279
|
+
for i in range(3): # 列
|
|
280
|
+
_ = self._frame.grid_columnconfigure(i, weight=1)
|
|
281
|
+
|
|
282
|
+
ttk.Label(self._frame, text=self._title, font=("SimHei", 12)).grid(row=0, column=0,
|
|
283
|
+
columnspan=3, pady=5)
|
|
284
|
+
|
|
285
|
+
self._year_scrollpicker.grid(row=1, column=0)
|
|
286
|
+
self._month_scrollpicker.grid(row=1, column=1)
|
|
287
|
+
self._day_scrollpicker.grid(row=1, column=2)
|
|
288
|
+
|
|
289
|
+
# 确认按钮和结果显示
|
|
290
|
+
self._confirm_btn = ttk.Button(self._frame, text="确认选择", command=self._confirm)
|
|
291
|
+
self._confirm_btn.grid(row=2, column=0, columnspan=3, pady=10)
|
|
292
|
+
|
|
293
|
+
self._result_var = tk.StringVar()
|
|
294
|
+
self._result_var.set(f"当前选择: {self._selected_year}-{self._selected_month:02d}-{self._selected_day:02d}")
|
|
295
|
+
self._result_label = ttk.Label(self._frame, textvariable=self._result_var, font=("SimHei", 12))
|
|
296
|
+
self._result_label.grid(row=3, column=0, columnspan=3, pady=5)
|
|
297
|
+
|
|
298
|
+
def _on_year_change(self, year: int):
|
|
299
|
+
self._selected_year = year
|
|
300
|
+
self._adjust_day_range()
|
|
301
|
+
self._update_result_preview()
|
|
302
|
+
|
|
303
|
+
def _on_month_change(self, month: int):
|
|
304
|
+
self._selected_month = month
|
|
305
|
+
self._adjust_day_range()
|
|
306
|
+
self._update_result_preview()
|
|
307
|
+
|
|
308
|
+
def _on_day_change(self, day: int):
|
|
309
|
+
self._selected_day = day
|
|
310
|
+
self._update_result_preview()
|
|
311
|
+
|
|
312
|
+
def _get_max_day(self) -> int:
|
|
313
|
+
""" 获取当月最大天数"""
|
|
314
|
+
return calendar.monthrange(self._selected_year, self._selected_month)[1]
|
|
315
|
+
|
|
316
|
+
def _adjust_day_range(self) -> None:
|
|
317
|
+
""" 调整日期以适应当前年月"""
|
|
318
|
+
max_day = self._get_max_day()
|
|
319
|
+
if self._selected_day > max_day:
|
|
320
|
+
self._selected_day = max_day
|
|
321
|
+
self._day_scrollpicker.update_data(self._selected_day)
|
|
322
|
+
day_list = list(range(1, max_day))
|
|
323
|
+
self._day_scrollpicker.update_val_list(day_list)
|
|
324
|
+
|
|
325
|
+
def _update_result_preview(self) -> None:
|
|
326
|
+
""" 更新结果预览"""
|
|
327
|
+
formatted_date: str = f"{self._selected_year}-{self._selected_month:02d}-{self._selected_day:02d}"
|
|
328
|
+
self._result_var.set(f"当前选择: {formatted_date}")
|
|
329
|
+
|
|
330
|
+
def _confirm(self) -> str:
|
|
331
|
+
""" 显示并返回最终选择的日期"""
|
|
332
|
+
formatted_date: str = f"{self._selected_year}-{self._selected_month:02d}-{self._selected_day:02d}"
|
|
333
|
+
self._result_var.set(f"已选择: {formatted_date}")
|
|
334
|
+
self._exit()
|
|
335
|
+
return formatted_date
|
|
336
|
+
|
|
337
|
+
def get_datestr(self) -> str:
|
|
338
|
+
""" 获取当前选择的日期"""
|
|
339
|
+
return f"{self._selected_year}-{self._selected_month:02d}-{self._selected_day:02d}"
|
|
340
|
+
|
|
341
|
+
def _exit(self):
|
|
342
|
+
self._master.grab_release()
|
|
343
|
+
self._master.destroy()
|
|
344
|
+
|
|
345
|
+
def _main_judge(self):
|
|
346
|
+
""" 判断窗口是否在最顶层"""
|
|
347
|
+
try:
|
|
348
|
+
if self._master.focus_displayof() is None \
|
|
349
|
+
or 'toplevel' not in str(self._master.focus_displayof()):
|
|
350
|
+
self._exit()
|
|
351
|
+
else:
|
|
352
|
+
_ = self._master.after(10, self._main_judge)
|
|
353
|
+
except:
|
|
354
|
+
_ = self._master.after(10, self._main_judge)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
class TimeScrollPickerCtrl(tkControl):
|
|
358
|
+
def __init__(self, parent: tk.Misc, owner: Dialog, idself: str,
|
|
359
|
+
width: int, height: int,
|
|
360
|
+
background: str = "white",
|
|
361
|
+
title: str = "", initial: str = ""):
|
|
362
|
+
self._master: tk.Misc = parent
|
|
363
|
+
self._owner: Dialog = owner
|
|
364
|
+
|
|
365
|
+
self._frame: tk.Frame = tk.Frame(self._master,
|
|
366
|
+
background=background,
|
|
367
|
+
width=width, height=height)
|
|
368
|
+
super().__init__(parent, "", idself, self._frame)
|
|
369
|
+
|
|
370
|
+
# 初始时间设置
|
|
371
|
+
if not initial:
|
|
372
|
+
now: datetime.datetime = datetime.datetime.now()
|
|
373
|
+
self._selected_hour: int = now.hour
|
|
374
|
+
self._selected_minute: int = now.minute
|
|
375
|
+
else:
|
|
376
|
+
now_list = initial.split(":")
|
|
377
|
+
self._selected_hour = int(now_list[0])
|
|
378
|
+
self._selected_minute = int(now_list[1])
|
|
379
|
+
|
|
380
|
+
hour_list = list(range(0, 24))
|
|
381
|
+
print(hour_list)
|
|
382
|
+
self._hour_scrollpicker: ScrollPicker[int] = ScrollPicker[int](self._frame,
|
|
383
|
+
hour_list, self._selected_hour,
|
|
384
|
+
self._on_hour_change,
|
|
385
|
+
background=background
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
minute_list = list(range(0, 60))
|
|
389
|
+
self._minute_scrollpicker: ScrollPicker[int] = ScrollPicker(self._frame,
|
|
390
|
+
minute_list, self._selected_minute,
|
|
391
|
+
self._on_minute_change,
|
|
392
|
+
background=background
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
self._result_var: tk.StringVar = tk.StringVar()
|
|
396
|
+
|
|
397
|
+
# 创建UI组件
|
|
398
|
+
self._create_widgets()
|
|
399
|
+
|
|
400
|
+
def _create_widgets(self) -> None:
|
|
401
|
+
# 配置网格权重,使布局更美观
|
|
402
|
+
for i in range(1): # 行
|
|
403
|
+
_ = self._frame.grid_rowconfigure(i, weight=1)
|
|
404
|
+
for i in range(2): # 列
|
|
405
|
+
_ = self._frame.grid_columnconfigure(i, weight=1)
|
|
406
|
+
|
|
407
|
+
# ttk.Label(self._frame, text=self._title, font=("SimHei", 12)).grid(row=0, column=0,
|
|
408
|
+
# columnspan=3, pady=5)
|
|
409
|
+
|
|
410
|
+
self._hour_scrollpicker.grid(row=0, column=1)
|
|
411
|
+
self._minute_scrollpicker.grid(row=0, column=2)
|
|
412
|
+
|
|
413
|
+
self._result_var = tk.StringVar()
|
|
414
|
+
self._result_var.set(f"当前选择: {self._selected_hour:02d}:{self._selected_minute:02d}")
|
|
415
|
+
|
|
416
|
+
def _on_hour_change(self, hour: int):
|
|
417
|
+
self._selected_hour = hour
|
|
418
|
+
self._update_result_preview()
|
|
419
|
+
time = datetime.time(self._selected_hour, self._selected_minute)
|
|
420
|
+
_ = self._owner.process_message(self._idself, event="select_time", val=time)
|
|
421
|
+
|
|
422
|
+
def _on_minute_change(self, minute: int):
|
|
423
|
+
self._selected_minute = minute
|
|
424
|
+
self._update_result_preview()
|
|
425
|
+
time = datetime.time(self._selected_hour, self._selected_minute)
|
|
426
|
+
_ = self._owner.process_message(self._idself, event="select_time", val=time)
|
|
427
|
+
|
|
428
|
+
def _update_result_preview(self) -> None:
|
|
429
|
+
""" 更新结果预览"""
|
|
430
|
+
formatted_time: str = f"{self._selected_hour:02d}:{self._selected_minute:02d}"
|
|
431
|
+
self._result_var.set(f"当前选择: {formatted_time}")
|
|
432
|
+
|
|
433
|
+
def get_time(self):
|
|
434
|
+
""" 获取当前选择的时间"""
|
|
435
|
+
return datetime.time(self._selected_hour, self._selected_minute)
|
|
436
|
+
|
|
437
|
+
def get_timestr(self) -> str:
|
|
438
|
+
""" 获取当前选择的时间字符串"""
|
|
439
|
+
return f"{self._selected_hour:02d}:{self._selected_minute:02d}"
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
class TimeScrollPickerDialog(Dialog):
|
|
443
|
+
def __init__(self, point: tuple[int, int] | None = None,
|
|
444
|
+
title: str = "", initial: str = ""):
|
|
445
|
+
super().__init__(title, 0, 0)
|
|
446
|
+
|
|
447
|
+
self._master: tk.Toplevel = tk.Toplevel()
|
|
448
|
+
self._master.withdraw()
|
|
449
|
+
|
|
450
|
+
self._frame: tk.Frame = tk.Frame(self._master)
|
|
451
|
+
|
|
452
|
+
self._title: str = title if title else "时间选择器"
|
|
453
|
+
|
|
454
|
+
# 初始时间设置
|
|
455
|
+
if not initial:
|
|
456
|
+
now: datetime.datetime = datetime.datetime.now()
|
|
457
|
+
selected_hour = now.hour
|
|
458
|
+
selected_minute = now.minute
|
|
459
|
+
else:
|
|
460
|
+
now_list = initial.split(":")
|
|
461
|
+
selected_hour = int(now_list[0])
|
|
462
|
+
selected_minute = int(now_list[1])
|
|
463
|
+
|
|
464
|
+
# ttk.Label(self._frame, text=self._title, font=("SimHei", 12)).grid(row=0, column=0, pady=5)
|
|
465
|
+
ttk.Label(self._frame, text=self._title, font=("SimHei", 12)).pack()
|
|
466
|
+
|
|
467
|
+
self._time_scrollpicker_ctrl: TimeScrollPickerCtrl = TimeScrollPickerCtrl(self._frame, self,
|
|
468
|
+
"tspSelectTime",
|
|
469
|
+
100, 50,
|
|
470
|
+
initial=f"{selected_hour}:{selected_minute}"
|
|
471
|
+
)
|
|
472
|
+
self._time_scrollpicker_ctrl.control.pack(pady=5)
|
|
473
|
+
|
|
474
|
+
confirm_btn = ttk.Button(self._frame, text="确认选择", command=self._confirm)
|
|
475
|
+
confirm_btn.pack()
|
|
476
|
+
|
|
477
|
+
self._result_var: tk.StringVar = tk.StringVar()
|
|
478
|
+
self._result_var.set(f"当前选择: {selected_hour:02d}:{selected_minute:02d}")
|
|
479
|
+
result_label = ttk.Label(self._frame, textvariable=self._result_var, font=("SimHei", 12))
|
|
480
|
+
result_label.pack(pady=(5,0))
|
|
481
|
+
|
|
482
|
+
self._frame.pack(expand = 1, fill = 'both')
|
|
483
|
+
|
|
484
|
+
self._master.overrideredirect(True)
|
|
485
|
+
self._master.update_idletasks()
|
|
486
|
+
width, height = self._master.winfo_reqwidth(), self._master.winfo_reqheight()
|
|
487
|
+
self.height: int = height
|
|
488
|
+
if point:
|
|
489
|
+
x, y = point[0], point[1]
|
|
490
|
+
else:
|
|
491
|
+
x, y = (self._master.winfo_screenwidth() - width)/2, \
|
|
492
|
+
(self._master.winfo_screenheight() - height)/2
|
|
493
|
+
self._master.geometry(f"{width}x{height}+{x}+{y}") # 窗口位置居中
|
|
494
|
+
_ = self._master.after(300, self._main_judge)
|
|
495
|
+
self._master.attributes('-topmost', True)
|
|
496
|
+
self._master.grab_set() # ensure all input goes to our window
|
|
497
|
+
self._master.deiconify()
|
|
498
|
+
self._master.focus_set()
|
|
499
|
+
self._master.wait_window()
|
|
500
|
+
|
|
501
|
+
@override
|
|
502
|
+
def _confirm(self, **kwargs: object):
|
|
503
|
+
self._exit()
|
|
504
|
+
return True, ""
|
|
505
|
+
|
|
506
|
+
def get_time(self):
|
|
507
|
+
""" 获取当前选择的时间"""
|
|
508
|
+
return self._time_scrollpicker_ctrl.get_time()
|
|
509
|
+
|
|
510
|
+
def get_timestr(self) -> str:
|
|
511
|
+
""" 获取当前选择的时间字符串"""
|
|
512
|
+
return self._time_scrollpicker_ctrl.get_timestr()
|
|
513
|
+
|
|
514
|
+
def _exit(self):
|
|
515
|
+
self._master.grab_release()
|
|
516
|
+
self._master.destroy()
|
|
517
|
+
|
|
518
|
+
def _main_judge(self):
|
|
519
|
+
""" 判断窗口是否在最顶层"""
|
|
520
|
+
try:
|
|
521
|
+
if self._master.focus_displayof() is None \
|
|
522
|
+
or 'toplevel' not in str(self._master.focus_displayof()):
|
|
523
|
+
self._exit()
|
|
524
|
+
else:
|
|
525
|
+
_ = self._master.after(10, self._main_judge)
|
|
526
|
+
except:
|
|
527
|
+
_ = self._master.after(10, self._main_judge)
|
|
528
|
+
|
|
529
|
+
@override
|
|
530
|
+
def process_message(self, idmsg: str, **kwargs: object):
|
|
531
|
+
match idmsg:
|
|
532
|
+
case "tspSelectTime":
|
|
533
|
+
time = self.get_time()
|
|
534
|
+
self._result_var.set(f"当前选择: {time.hour:02d}:{time.minute:02d}")
|
|
535
|
+
case _:
|
|
536
|
+
return super().process_message(idmsg, **kwargs)
|
|
537
|
+
return True
|
|
538
|
+
|
|
539
|
+
@override
|
|
540
|
+
def destroy(self, **kwargs: object):
|
|
541
|
+
pass
|
|
542
|
+
|
|
543
|
+
if __name__ == "__main__":
|
|
544
|
+
class ScrollPickerCtrl_test():
|
|
545
|
+
def __init__(self):
|
|
546
|
+
|
|
547
|
+
self._root: tk.Tk = tk.Tk()
|
|
548
|
+
|
|
549
|
+
# 设置tk窗口在屏幕中心显示
|
|
550
|
+
sw = self._root.winfo_screenwidth() # 得到屏幕宽度
|
|
551
|
+
sh = self._root.winfo_screenheight() # 得到屏幕高度
|
|
552
|
+
ww = 300 # 设置窗口 宽度
|
|
553
|
+
wh = 120 # 设置窗口 高度
|
|
554
|
+
|
|
555
|
+
x = (sw - ww) / 2
|
|
556
|
+
y = (sh - wh) / 2
|
|
557
|
+
self._root.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
|
|
558
|
+
|
|
559
|
+
self._frame: tk.Frame = tk.Frame(self._root)
|
|
560
|
+
self._frame.pack()
|
|
561
|
+
|
|
562
|
+
self._label_date: tk.Label = tk.Label(self._frame, width=10,
|
|
563
|
+
text="选择日期", font="宋体, 12", justify='center')
|
|
564
|
+
self._label_date.grid(row=0, column=0, padx=5, pady=5)
|
|
565
|
+
|
|
566
|
+
self._var_date: tk.StringVar = tk.StringVar()
|
|
567
|
+
self._entry_date: tk.Entry = tk.Entry(self._frame, width=10,
|
|
568
|
+
textvariable=self._var_date, font="宋体, 12",
|
|
569
|
+
justify='center', )
|
|
570
|
+
self._entry_date.grid(row=0, column=1, padx=5, pady=5)
|
|
571
|
+
|
|
572
|
+
self._button_seldate: tk.Button = tk.Button(self._frame, width=2,
|
|
573
|
+
text='*', font="宋体, 12",
|
|
574
|
+
justify='center',
|
|
575
|
+
command=lambda: self.get_date(self._entry_date.winfo_rootx(),
|
|
576
|
+
self._entry_date.winfo_rooty() + 20))
|
|
577
|
+
self._button_seldate.grid(row=0, column=2, padx=5, pady=5)
|
|
578
|
+
|
|
579
|
+
self._label_time: tk.Label = tk.Label(self._frame, width=10,
|
|
580
|
+
text="选择时间", font="宋体, 12", justify='center')
|
|
581
|
+
self._label_time.grid(row=1, column=0, padx=5, pady=5)
|
|
582
|
+
|
|
583
|
+
self._var_time: tk.StringVar = tk.StringVar()
|
|
584
|
+
self._entry_time: tk.Entry = tk.Entry(self._frame, width=10,
|
|
585
|
+
textvariable=self._var_time, font="宋体, 12",
|
|
586
|
+
justify='center', )
|
|
587
|
+
self._entry_time.grid(row=1, column=1, padx=5, pady=5)
|
|
588
|
+
|
|
589
|
+
self._button_seltime: tk.Button = tk.Button(self._frame, width=2,
|
|
590
|
+
text='*', font="宋体, 12",
|
|
591
|
+
justify='center',
|
|
592
|
+
command=lambda: self.get_time(self._entry_time.winfo_rootx(),
|
|
593
|
+
self._entry_time.winfo_rooty() + 20))
|
|
594
|
+
self._button_seltime.grid(row=1, column=2, padx=5, pady=5)
|
|
595
|
+
|
|
596
|
+
self._root.mainloop()
|
|
597
|
+
|
|
598
|
+
def get_date(self, x: int, y: int): # x, y位Entry的坐标位置
|
|
599
|
+
# 接收弹窗的数据
|
|
600
|
+
res = self.ask_date(x, y)
|
|
601
|
+
self._var_date.set(res)
|
|
602
|
+
|
|
603
|
+
def ask_date(self, x: int, y: int):
|
|
604
|
+
scrollpicker = DateScrollPickerDialog((x, y))
|
|
605
|
+
return scrollpicker.get_datestr()
|
|
606
|
+
|
|
607
|
+
def get_time(self, x: int, y: int): # x, y位Entry的坐标位置
|
|
608
|
+
# 接收弹窗的数据
|
|
609
|
+
res = self.ask_time(x, y)
|
|
610
|
+
self._var_time.set(res)
|
|
611
|
+
|
|
612
|
+
def ask_time(self, x: int, y: int):
|
|
613
|
+
scrollpicker = TimeScrollPickerDialog((x, y), "wahaha")
|
|
614
|
+
return scrollpicker.get_timestr()
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
app = ScrollPickerCtrl_test()
|