ttkbootstrap 实现日期选择器, 开始和结束时间
1. 展示

2. 打印

3. 源码
from datetime import datetime
import ttkbootstrap as ttkclass DateTimeEntryStart(ttk.Frame):def __init__(self, master=None, **kwargs):super().__init__(master, **kwargs)self.date_entry = ttk.DateEntry(self, bootstyle="secondary", width=8)self.date_entry.pack(side="left", padx=2)self.popup = ttk.Frame(self.date_entry)self.popup.pack(side="top", pady=5)self.hour_spinbox = ttk.Spinbox(self.popup, from_=0, to=23, width=2)self.hour_spinbox.set(23) self.hour_spinbox.grid(row=0, column=0, padx=2, pady=5)self.minute_spinbox = ttk.Spinbox(self.popup, from_=0, to=59, width=2)self.minute_spinbox.set(59) self.minute_spinbox.grid(row=0, column=1, padx=2, pady=5)def get_datetime(self):try:date = self.date_entry.entry.get()hour = self.hour_spinbox.get()minute = self.minute_spinbox.get()datetime_str = f"{date} {hour}:{minute}"dt = datetime.strptime(datetime_str, "%Y/%m/%d %H:%M")return dt.strftime("%Y-%m-%d %H:%M:%S")except ValueError:return "Invalid date or time"class DateTimeEntryEnd(ttk.Frame):def __init__(self, master=None, **kwargs):super().__init__(master, **kwargs)self.date_entry = ttk.DateEntry(self, bootstyle="secondary", width=8)self.date_entry.pack(side="left", padx=2)self.popup = ttk.Frame(self.date_entry)self.popup.pack(side="top", pady=5)self.hour_spinbox = ttk.Spinbox(self.popup, from_=0, to=23, width=2)self.hour_spinbox.set(23) self.hour_spinbox.grid(row=0, column=0, padx=2, pady=5)self.minute_spinbox = ttk.Spinbox(self.popup, from_=0, to=59, width=2)self.minute_spinbox.set(59) self.minute_spinbox.grid(row=0, column=1, padx=2, pady=5)def get_datetime(self):try:date = self.date_entry.entry.get()hour = self.hour_spinbox.get()minute = self.minute_spinbox.get()datetime_str = f"{date} {hour}:{minute}"dt = datetime.strptime(datetime_str, "%Y/%m/%d %H:%M")return dt.strftime("%Y-%m-%d %H:%M:%S")except ValueError:return "Invalid date or time"root = ttk.Window(title="日期时间选择器")
root.geometry("800x600")
frame = ttk.Frame(root)
frame.pack()
datetime_entry_start = DateTimeEntryStart(frame)
datetime_entry_start.pack(side="left", padx=2)label_to_time = ttk.Label(frame, text="至")
label_to_time.pack(side="left", padx=2)datetime_entry_end = DateTimeEntryEnd(frame)
datetime_entry_end.pack(side="left", padx=2)
print(datetime_entry_start.get_datetime())
print(datetime_entry_end.get_datetime())root.mainloop()